Android 在复选框选择上选择列表视图中的所有项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24690605/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:35:56  来源:igfitidea点击:

Selecting All Items in a Listview on checkbox select

androidandroid-listviewandroid-checkbox

提问by Addy

I am using simple listView with simple_list_item_multiple_choiceI have added a checkbox and on its checked event want all list items to get selected and on unchecked all items to get unselected.. Here is the code..

我正在使用简单的 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
                }

            }
                }); 

回答by Soumil Deshpande

for ( int i=0; i < listview.getChildCount(); i++) {
   listview.setItemChecked(i, true);
}

回答by BAIJU SHARMA

Call a method from a ListAdapteron a button click or onOptionsItemSelected(MenuItem item).

ListAdapter按钮单击或调用方法onOptionsItemSelected(MenuItem item)

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

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

And then,

进而,

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

回答by Dusha Kucher

Select all / Deselect all / Inverse all

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

For Activity

对于活动

@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 - ListView ID LinearLayout or RelativeLayout - see root in your item cbFileDownload - CheckBox ID see in your item

lvDownload - ListView ID LinearLayout 或 RelativeLayout - 在您的项目中查看根 cbFileDownload - CheckBox ID 在您的项目中查看

And Menu:

和菜单:

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

回答by Masht Metti

I think you should run this long-running task off the UI thread. When you click button in OnClickListener:

我认为您应该在 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();    

and in onCreate() :

在 onCreate() 中:

this.mHandler = new Handler();

Each item in list view should be Checkable like CheckableRelativeLayout that implements Checkable interface.

列表视图中的每个项目都应该像 CheckableRelativeLayout 实现 Checkable 接口一样可检查。