带有列表视图的警报对话框,选中所有按钮并取消选中所有按钮 android

alertdialog with listview, check all and uncheck all button android

到目前为止已经为带有列表视图和提交按钮的警报对话框编写了代码,但现在我想将 "check all" 和 "uncheck all" 按钮添加到我的列表视图中是否可以在警报对话框中添加:listview,checkall 按钮,取消选中所有按钮并将带有 eventListeners 的按钮提交给所有按钮加上列表视图适配器?。我冲浪了很多,我所能找到的只是只有列表视图和提交按钮的警报对话框。

这是我的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(
                        getActivity());
                builder.setTitle("Select Category List");

                final ListView modeList = new ListView(getActivity());

                // Creating a button - submit
                Button btnDone = new Button(getActivity());
                btnDone.setText("Done");
                btnDone.setTextColor(getActivity().getResources().getColor(
                        android.R.color.white));    
                btnDone.setBackgroundResource(R.drawable.donebtn_with_bg);    
                final ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(
                        getActivity(),
                        android.R.layout.simple_list_item_multiple_choice,
                        android.R.id.text1, EavesList);
                modeList.addFooterView(btnDone);    
                modeList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);    
                modeList.setAdapter(modeAdapter);                   
                for (int i = 0; i < EavesList.size(); i++) {    
                    if (selectedItems.size() != 0) {                            
                        for (int j = 0; j < selectedItems.size(); j++) {
                            if (EavesList.get(i).contains(selectedItems.get(j))) {
                                modeList.setItemChecked(i, true);    
                                j = selectedItems.size() + 2;
                            } else {
                                if (j + 1 == selectedItems.size()) {
                                    modeList.setItemChecked(i, false);
                                }
                            }
                        }
                    } else {    
                        Log.e("Selected Item ", "No Values");    
                    }
                }                   
                builder.setView(modeList);    
                final Dialog dialog = builder.create();    
                btnDone.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        selectedItems.clear();
                        eavesdropping.setText("");
                        SparseBooleanArray checked = modeList
                                .getCheckedItemPositions();
                        selected_community_id = new String[checked.size()];
                        String[] evas_id;
                        for (int i = 0; i < checked.size(); i++) {
                            int position = checked.keyAt(i);
                            if (checked.valueAt(i))
                                selectedItems.add(EavesList.get(position));                             
                            selected_community_id[i] = EavesList_id
                                    .get(position);
                        }    
                            for (int i = 0; i < selected_community_id.length; i++) {                            
                            eavesdropping_hide.append(selected_community_id[i]);
                            if (i != selected_community_id.length - 1) {
                                eavesdropping_hide.append(",");
                            }    
                        }    
                        String[] outputStrArr = new String[selectedItems.size()];    
                        for (int i = 0; i < selectedItems.size(); i++) {
                            outputStrArr[i] = selectedItems.get(i);    
                            eavesdropping.append(outputStrArr[i]);
                            eavesdropping.append(" | ");    
                        }    
                        dialog.dismiss();    
                    }
                });    
                dialog.show();

在上面的代码中,我使用 Listview 的 addFooterView 方法添加了提交按钮..

这是我想要的:

所以与此相关的任何教程或任何想法或代码都会对我有所帮助。提前致谢,很抱歉 post.

是的,您可以 that.On 检查所有单击您需要调用适配器的值,这些值将告诉您需要检查 adapter.Similarly 中的所有复选框,并取消选中大小写。 两次都需要通知适配器重新设置值。

你可以试试这个: 当您在 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(position, true);  
                      }
               });
          }
    }
}).start();     

并在 onCreate() 中:

this.mHandler = new Handler();

列表视图中的每个项目都应该是可检查的,例如实现 Checkable 接口的 CheckableRelativeLayout。