如何使用listview复选框。在此我想在复选框点击上执行一些任务。我没有得到如何使用复选框选择特定的id或listitem值。
答案 0 :(得分: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();
}
});
}
简要教程参考this链接。
我希望这会对你有所帮助。
答案 1 :(得分: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());
}
}
答案 2 :(得分:0)
这可以通过使用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复选框和CompoundButtons(CheckBox超类)的文档可以在这里找到:
Android文档非常有用,有时很难对其进行加密并找到您想要的内容。希望这能回答你的问题!