Android AlertDialog多选模式问题

时间:2014-05-13 03:52:37

标签: android android-alertdialog

我已将AlertDialog设置为CHOICE_MODE_MULTIPLE,并从API获取列表的数据。它工作得很好,但是,我现在遇到两个问题。

  1. 第一个更严重:在我检查项目后,然后单击确定,有时当我再次返回对话框时,它会取消选中我所有选中的项目。当我测试时,这种情况只发生了10-20%,所以我不知道为什么会这样。我试图将值保存到我的类中的全局变量中,或者是我在应用程序刚启动时创建的公共静态变量,但仍然没有运气。
  2. 当我点击"取消"时,已检查的项目仍会更新(它不应该更新)。
  3. 我很难对付这两个问题,非常感谢任何帮助。

    编辑:这是我的自定义对话框:

    public class CustomMultichoiceDialog {
    
        Set<String> selectedString = new HashSet<String>();
    
        private String[] items;
    
        private AlertDialog dialog;
    
        private Builder builder;
    
        private Context context;
    
        private TestAdapter adapter;
    
        public CustomMultichoiceDialog(Context context, String[] items) {
    
            this.context = context;
            this.items = items;
            builder = new AlertDialog.Builder(context);
            this.adapter = new TestAdapter(context, android.R.layout.simple_list_item_checked, this.items);
            builder.setAdapter(this.adapter, null);
    
        }
    
        public AlertDialog show() {
    
            if (this.dialog == null) {
                this.create();
            }
    
            this.adapter.notifyDataSetChanged();
    
            this.dialog.show();
    
            return this.dialog;
        }
    
        public String[] getSelectedItems() {
            String[] result = new String[this.dialog.getListView().getCheckedItemCount()];
    
            SparseBooleanArray checked = this.dialog.getListView().getCheckedItemPositions();
    
            int k = 0;
            for (int i = 0; i < this.items.length; i++) {
                if (checked.get(i)) {
                    result[k++] = this.items[i];
                }
            }
    
            return result;
        }
    
        public String getSelectedItemAsString() {
            return Arrays.toString(this.getSelectedItems()).replace("[", "").replace("]", "")
          .replace(", ", ",");
        }
    
        public AlertDialog create() {
            this.dialog = builder.create();
            this.dialog.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    
            this.dialog.getListView().setSelector(new ColorDrawable(Color.TRANSPARENT));
            return this.dialog;
        }
    
        public void setPositiveButton(final Callbacks callbacks) {
            this.builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String[] selected = getSelectedItems();
                    callbacks.run(selected, getSelectedItemAsString());
                    cacheSelectedItems(selected);
                }
            });
        }
    
        public void setNegativeButton(final Callbacks callbacks) {
            this.builder.setNegativeButton(this.context.getString(R.string.cancel),
            new DialogInterface.OnClickListener() {
    
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
    
                  }
            });
        }
    
        private void cacheSelectedItems(String[] selected) {
            selectedString.addAll(Arrays.asList(selected));
        }
    
        public interface Callbacks {
            public void run(String[] selectedItems, String selectedItemAsString);
        }
    
        class TestAdapter extends ArrayAdapter<String> {
    
            public TestAdapter(Context context, int resource, String[] objects) {
                super(context, resource, objects);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
    
                ViewHolder holder = (ViewHolder) view.getTag();
    
                if (holder == null) {
                    holder = new ViewHolder();
                    holder.text = ((CheckedTextView) view.findViewById(android.R.id.text1));
                    holder.text.setCheckMarkDrawable(R.drawable.checkbox);
                }
    
                if(selectedString!=null){
                    if(selectedString.contains(holder.text.getText().toString())) {
                    holder.text.setSelected(true);
                    holder.text.setChecked(true);
                    }
                }
    
                return view;
            }
    
            class ViewHolder {
                CheckedTextView text;
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
        }
    
    }
    

0 个答案:

没有答案