【问题标题】:how to use listview with checkbox and perform some task on checkbox click?如何使用带有复选框的列表视图并在单击复选框时执行一些任务?
【发布时间】:2013-04-11 04:13:04
【问题描述】:

如何在 listview 中使用复选框。在此我想在单击复选框时执行一些任务。我不知道如何使用复选框选择特定的 id 或 listitem 值。

【问题讨论】:

  • 发布您的代码。到目前为止你尝试了什么..这是android中非常基本的问题。你会在谷歌搜索中得到很多答案...

标签: java android kindle-fire


【解决方案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链接。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    试试这个, 列表适配器:-

    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);
    

    在你想从列表视图中获取检查值之后,

    for(int i=0;i<listView.getCount();i++)
    {
        ArrayListObjects obj=(ArrayListObjects)listView.getItemAtPosition(i);
        if(obj.isItemChecked())
        {   
           Log.e("checked items - ",obj.getItemName());
        }
    }
    

    【讨论】:

      【解决方案3】:

      这可以通过使用 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 文档非常有用,有时很难通过密码找到您想要的内容。希望这能回答您的问题!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        • 1970-01-01
        • 2012-03-09
        • 1970-01-01
        • 2014-04-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多