Android:Alert Dialog返回Object

时间:2016-09-01 21:50:04

标签: android callback dialog android-dialog

我的活动中有很多文字观点。 例如,如果我单击颜色文本视图。将出现一个对话框,其中包含从mColourArray填充的颜色列表。然后我应该能够选择颜色并在mColourArraySelected中返回结果。 mAlphabetArray和mAlphabetArraySelected也是如此。

下面的问题是当我尝试在案例陈述中指定mColourArraySelected时它是空的,因为在onClick事件中没有返回MultiDialog中的resultList。

它不会让我改变onClick返回List所以我该如何实现回调?

    @Override
public void onClick(View view) {

    switch (view.getId()) {
        case R.id.textViewColour:
        mColourArraySelected = MultiDialog(mColourArray);
            break;
        case R.id.textViewAlphabet:
            mAlphabetArraySelected = MultiDialog(mAlphabetArray);
            break;
        break;
    }
}

    public List<String> MultiDialog(final List<String> list ) {

    resultList.clear();

    final String[] array = new String [list.size()] ;
    for (int i=0; i<list.size(); i++) {
        array[i]=list.get(i);
    }

    final ArrayList mSelectedItems = new ArrayList();  // Where we track the selected items
    AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);
    // Set the dialog title
    builder.setTitle("multi select")
            // Specify the list array, the items to be selected by default (null for none),
            // and the listener through which to receive callbacks when items are selected
            .setMultiChoiceItems(array, null,
                    new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which,
                                            boolean isChecked) {
                            if (isChecked) {
                                // If the user checked the item, add it to the selected items
                                mSelectedItems.add(which);
                            } else if (mSelectedItems.contains(which)) {
                                // Else, if the item is already in the array, remove it
                                mSelectedItems.remove(Integer.valueOf(which));
                            }
                        }
                    })
            // Set the action buttons
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // User clicked OK, so save the mSelectedItems results somewhere
                    // or return them to the component that opened the dialog
                    for (int i=0; i<mSelectedItems.size(); i++) {
                        int pos = (int) mSelectedItems.get(i);
                        resultList.add(list.get(pos));
                    }

                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                }
            });
    AlertDialog dialog = builder.create();
    dialog.show();
    return resultList;

}

0 个答案:

没有答案