【问题标题】:Selecting All Items in a Listview on checkbox select在复选框中选择列表视图中的所有项目
【发布时间】:2014-09-01 15:48:13
【问题描述】:

我正在使用简单的 listView 和 simple_list_item_multiple_choice 我添加了一个复选框,并在其选中的事件上希望所有列表项都被选中,并且在未选中的情况下所有项目都被取消选中.. 这是代码..

CheckBox select_all = (CheckBox) dialog.findViewById(R.id.chk_all);
        arrayAdapter = new ArrayAdapter<String>
        (ctx,android.R.layout.simple_list_item_multiple_choice,readyToDownload );
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lv.setAdapter(arrayAdapter);

   select_all.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
            if(select_all.isChecked())
            {
                // check all list items
            }
            if(!select_all.isChecked())
                {
                    //  unselect all list items
                }

            }
                }); 

【问题讨论】:

标签: android android-listview android-checkbox


【解决方案1】:
for ( int i=0; i < listview.getChildCount(); i++) {
   listview.setItemChecked(i, true);
}

【讨论】:

  • 请用一段代码澄清一下。
  • 谢谢,如果对你有用,请接受答案
  • 这只会点击当前显示的项目,并且会在滚动和回收时出现问题。
【解决方案2】:

我认为您应该在 UI 线程之外运行这个长时间运行的任务。当您单击 OnClickListener 中的按钮时:

new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < list.getAdapter().getCount(); i++) {
                            final int position = i;
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    list.setItemChecked(pos, true);  
                                }
                            });
                        }
                    }
                }).start();    

在 onCreate() 中:

this.mHandler = new Handler();

列表视图中的每个项目都应该是 Checkable ,就像实现 Checkable 接口的 CheckableRelativeLayout 一样。

【讨论】:

    【解决方案3】:

    单击按钮时从ListAdapteronOptionsItemSelected(MenuItem item) 调用方法。

    case  R.id.selectAll:
                    listAdapterData.selectAll();
                    return true;
    
    case  R.id.unselectAll:
                    listAdapterData.unselectAll();
                     return true;
    

    然后,

    public class ListAdapterData extends BaseAdapter {
        Context cntxts;
        private LayoutInflater mInflater;
        private ArrayList objects;
        public SparseBooleanArray mSelectedItemsIds;
        boolean[] checkBoxState;
        boolean IsVisibleMain;
    
       public ListAdapterData(Context context, ArrayList objAll, boolean IsVisible) {
            mInflater = LayoutInflater.from(context);
            this.cntxts = context;
            this.objects = objAll;
            this.mSelectedItemsIds = new SparseBooleanArray();
            checkBoxState = new boolean[objects.size()];
            this.IsVisibleMain = IsVisible;
        }
    
        public void selectAll() {
            for (int i = 0; i < checkBoxState.length; i++) {
                checkBoxState[i] = true;
            }
            notifyDataSetChanged();
        }
    
        public void unselectAll() {
            for (int i = 0; i < checkBoxState.length; i++) {
                checkBoxState[i] = false;
            }
            notifyDataSetChanged();
        }
    }
    

    【讨论】:

      【解决方案4】:

      全选/取消全选/全部反转

      对于活动

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      	// Handle action bar item clicks here. The action bar will
      	// automatically handle clicks on the Home/Up button, so long
      	// as you specify a parent activity in AndroidManifest.xml.
      	int id = item.getItemId();
      
      	if (id == R.id.action_select_all) {
      		for(int i=0; i < lvDownload.getChildCount(); i++){
      			LinearLayout itemLayout = (LinearLayout)lvDownload.getChildAt(i);
      			CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.cbFileDownload);
      			cb.setChecked(true);
      		}
      		return true;
      	} else if (id == R.id.action_deselect_all) {
      		for(int i=0; i < lvDownload.getChildCount(); i++){
      			LinearLayout itemLayout = (LinearLayout)lvDownload.getChildAt(i);
      			CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.cbFileDownload);
      			cb.setChecked(false);
      		}
      		return true;
      	} else if (id == R.id.action_inverse_all) {
      		for(int i=0; i < lvDownload.getChildCount(); i++){
      			LinearLayout itemLayout = (LinearLayout)lvDownload.getChildAt(i);
      			CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.cbFileDownload);
      			cb.setChecked(!cb.isChecked());
      		}
      		return true;
      	}
      
      	return super.onOptionsItemSelected(item);
      }

      lvDownload - 列表视图 ID LinearLayout 或 RelativeLayout - 查看项目中的根 cbFileDownload - 在您的项目中查看复选框 ID

      还有菜单:

      <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          tools:context="ua.com.pultok.AboutActivity">
          <item
              android:id="@+id/action_select_all"
              android:orderInCategory="100"
              android:title="@string/action_select_all"
              app:showAsAction="never" />
          <item
              android:id="@+id/action_deselect_all"
              android:orderInCategory="100"
              android:title="@string/action_deselect_all"
              app:showAsAction="never" />
          <item
              android:id="@+id/action_inverse_all"
              android:orderInCategory="100"
              android:title="@string/action_inverse_all"
              app:showAsAction="never" />
      </menu>

      【讨论】:

        猜你喜欢
        • 2017-05-05
        • 1970-01-01
        • 2016-02-13
        • 1970-01-01
        • 2017-08-27
        • 1970-01-01
        • 2010-10-30
        • 2017-11-23
        • 1970-01-01
        相关资源
        最近更新 更多