如何在列表视图中使用复选框。在此我想在单击复选框时执行一些任务。我没有得到如何使用复选框选择特定的 id 或 listitem 值。
问问题
428 次
3 回答
1
试试这个,列出适配器:-
public class CheckBoxInListAdapter extends BaseAdapter{
private Context context=null;
private List<ArrayListObjects> searchArrayList=null;
private LayoutInflater mInflater=null;
private ViewHolder holder=new ViewHolder();
public CheckBoxInListAdapter(Context context, List<ArrayListObjects> mData)
{
this.context=context;
searchArrayList = mData;
mInflater = LayoutInflater.from(context);
}
public int getCount()
{
return searchArrayList.size();
}
public Object getItem(int position)
{
return searchArrayList.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.sample_layout,null);
holder=new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.textView);
holder.itemNormalChecked=(CheckBox)convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(searchArrayList.get(position).getItemName());
holder.itemNormalChecked.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(searchArrayList.get(position).isItemChecked())
{
searchArrayList.get(position).setItemChecked(false);
}
else
{
searchArrayList.get(position).setItemChecked(true);
}
}
});
if(searchArrayList.get(position).isItemChecked())
{
holder.itemNormalChecked.setChecked(true);
}
else
{
holder.itemNormalChecked.setChecked(false);
}
return convertView;
}
static class ViewHolder
{
TextView textView=null;
CheckBox itemNormalChecked=null;
}
}
布局:- sample_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/checkBox"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"/>
</RelativeLayout>
和 ArrayListObjects.java
public class ArrayListObjects {
private boolean isItemChecked=false;
private String itemName="null";
public boolean isItemChecked() {
return isItemChecked;
}
public void setItemChecked(boolean isItemChecked) {
this.isItemChecked = isItemChecked;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
然后在你的活动中,
ArrayList<ArrayListObjects> arrayListObjects=new ArrayList<ArrayListObjects>();
for(int i=0;i<15;i++)
{
arrayListObjects.add((new ArrayListObjects()).setItemName("value -"+String.valueOf(position);
}
listView.setAdapter(new CheckBoxInListAdapter(activityName.this,arrayListObjects);
在你想从 listview 中获取检查值之后,
for(int i=0;i<listView.getCount();i++)
{
ArrayListObjects obj=(ArrayListObjects)listView.getItemAtPosition(i);
if(obj.isItemChecked())
{
Log.e("checked items - ",obj.getItemName());
}
}
于 2013-04-11T05:11:25.367 回答
1
只需覆盖getview
阵列适配器的方法。并用于viewholder
获取您checkbox
的自定义列表。
示例代码:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.your_layout, null);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
}
});
}
简要教程参考此链接。
我希望这能帮到您。
于 2013-04-11T04:23:33.157 回答
0
这可以通过使用 using 来完成CompoundButton.OnCheckChangedListener
。您可以像这样内联设置此侦听器:
//Create a CheckBox object from your CheckBox id that is in the ListView in your xml layout file
CheckBox foo = (CheckBox) findViewById(R.id.foo);
foo.setOnCheckedChangedListener( new CompoundButton.OnCheckedChangedListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked = true){
// perform some operations when checked
} else {
// perform some operations when unchecked
}
});
可以在此处找到有关 Android Checkboxes 和 CompoundButtons(CheckBox 超类)的文档:
Android 文档非常有用,有时很难对其进行加密并找到您想要的内容。希望这能回答你的问题!
于 2013-04-11T04:37:55.783 回答