使用listview的alertdialog,检查所有并取消选中所有按钮android

时间:2015-01-29 05:19:32

标签: android listview android-listview android-alertdialog

到目前为止已经使用listview和提交按钮为alertdialog编码了,但现在我想在我的listview中添加“check all”和“uncheck all”按钮是否可以添加alertdialog:listview,checkall按钮,取消选中所有按钮和使用eventListeners向所有按钮加listview适配器提交按钮?我冲浪了很多我能找到的只有listview和提交按钮的alertdialog。

这是我的代码:

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方法..

这是我想要的:alertdialog must look same as this image

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

2 个答案:

答案 0 :(得分:1)

是的,你可以这样做。检查所有点击你需要调用适配器的值,告诉你需要检查适配器中的所有复选框。类似于取消选中案例。 两次都需要通知适配器再次设置值。

答案 1 :(得分:0)

你可以试试这个: 单击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();

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