Android 带有复选框单选的自定义列表视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12001143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom Listview with CheckBox single selection
提问by Rahul Patel
Here I am creating custom listview with checkbox / RadioButton. I got this but I need the single selection for that.
在这里,我正在使用复选框/单选按钮创建自定义列表视图。我得到了这个,但我需要一个单一的选择。
I try using this lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
but it not works for me. Is there any other solutions for that then please let me know.
我尝试使用它,lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
但它对我不起作用。有没有其他解决方案,然后请告诉我。
main.java
主程序
private ImageAdapter adapter;
private static String month[] = {"January","February","March","April","May",
"June","July","August","September",
"October","November","December"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lstvw = (ListView) findViewById(R.id.listView);
adapter = new ImageAdapter(this, month);
lstvw.setAdapter(adapter);
lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lstvw.setOnItemClickListener(this);
}
I have no idea for the how to add code for the checkbox into the adapter class.please check the adapter class as below.
我不知道如何将复选框的代码添加到适配器类中。请检查适配器类,如下所示。
ImageAdapter.class
图像适配器类
public class ImageAdapter extends BaseAdapter{
public String title[];
public String description[];
public Activity context;
public LayoutInflater inflater;
public ImageAdapter(Activity context,String[] title) {
super();
this.context = context;
this.title = title;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
TextView txtViewTitle;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.listitem, null);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.lstvw_textView);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
holder.txtViewTitle.setText(title[position]);
return convertView;
}
}
EDIT:
编辑:
Listitem.xml
列表项.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/lstvw_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/itemCheckBox"
android:padding="8dp"
android:text="hello world" />
<CheckBox
android:id="@+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right" />
回答by swiftBoy
Here is what i would do if i need to select only single item at a time.
如果我一次只需要选择一个项目,我会这样做。
Home.java (Activity)
Home.java(活动)
package com.lvcheck.activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class Home extends Activity
{
private ListView lvCheckBox;
private Button btnCheckAll, btnClearALl;
private String[] arr = {"One", "Two", "Three", "Four", "Five", "Six"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnCheckAll = (Button)findViewById(R.id.btnCheckAll);
btnClearALl = (Button)findViewById(R.id.btnClearAll);
lvCheckBox = (ListView)findViewById(R.id.lvCheckBox);
lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvCheckBox.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, arr));
btnCheckAll.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
{
lvCheckBox.setItemChecked(i, true);
}
}
});
btnClearALl.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
for(int i=0 ; i < lvCheckBox.getAdapter().getCount(); i++)
{
lvCheckBox.setItemChecked(i, false);
}
}
});
}
}
and my (main.xml) XML fileshould like this
我的(main.xml)XML 文件应该是这样的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:text="Check All"
android:layout_marginRight="7dip"
android:id="@+id/btnCheckAll"
android:layout_height="wrap_content">
</Button>
<Button
android:layout_width="wrap_content"
android:text="Clear All"
android:id="@+id/btnClearAll"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:id="@+id/lvCheckBox"
android:fadingEdge="none"
android:cacheColorHint="@null"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
so the output will be like this way..
所以输出会像这样..
Source: here
来源:这里
let me know if you have any trouble regarding this.
如果您对此有任何问题,请告诉我。
Edit:check this useful link: Custom Single choice ListView
编辑:检查这个有用的链接:自定义单选ListView
回答by Akshay
You can use CheckBox
in your adapter class. Try this.
您可以CheckBox
在您的适配器类中使用。尝试这个。
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
inflater = LayoutInflater.from(adapterContext);
convertView = inflater.inflate(R.layout.view, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.txtName);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(viewHolder);
}
final ViewHolder holder = (ViewHolder) convertView.getTag();
holder.name.setText(collectContactList.get(position).getName());
holder.checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
if (cb.isChecked() == true)
{
// Here you will get list of checked item.
}
else
{
// Here you will get list of unchecked item.
}
}
});
Hope this will help you.
希望这会帮助你。