Java 从“多选”对话框返回选中项

Java 从“多选”对话框返回选中项,java,android,Java,Android,通过阅读,我成功地创建了一个多选对话框。然而,有一点仍然让我完全困惑。当用户单击“确定”时,如何将其返回到父活动 我指的是这个特别的评论: // Set the action buttons .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog

通过阅读,我成功地创建了一个多选对话框。然而,有一点仍然让我完全困惑。当用户单击“确定”时,如何将其返回到父活动

我指的是这个特别的评论:

 // 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
               ...
           }
我将如何“将它们返回到打开对话框的组件”

我的活动中的对话调用:

 public void chooseTeam(View v) {;
    DialogFragment newDialog = MultiChoiceDialog.newInstance(teamNamesArray);
    newDialog.show(getFragmentManager(), "Choose_team");

}
和我的对话框代码:

public Dialog onCreateDialog(Bundle savedInstanceState) {
    final String[] team = getArguments().getStringArray("team");
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.choose_team).setMultiChoiceItems(team, null, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            if (isChecked) {
                list.add(team[which]);
            } else if (list.contains(team[which])) {
                list.remove(team[which]);
            }
        }
    }).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String selections = "";
            for (String ms : list) {
                selections = selections + "\n" + ms;
            }
            Toast.makeText(getActivity(), "Team Selected: " + selections,
                    Toast.LENGTH_LONG).show();
            AddTaskActivity.chosenTeam =  list;
        }
    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            Toast.makeText(getActivity(), "Cancelled.", Toast.LENGTH_SHORT).show();
        }
    });

    return builder.create();
}

您可以将ArrayList设置为类成员变量,如
private ArrayList mSelectedItems;

请参见下面的示例代码

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private ArrayList mSelectedItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mSelectedItems = new ArrayList();
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            // Set the dialog title
            builder.setTitle(R.string.pick_toppings)
                    // 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(R.array.toppings, 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

// in this case the OK button has more or less no function but if want save the check options in database or shared preference, you can put your code here. So it depends on your use case.

                        }
                    })
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                        }
                    });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    });

  }
}

您可以将ArrayList设置为类成员变量,如
private ArrayList mSelectedItems;

请参见下面的示例代码

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private ArrayList mSelectedItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mSelectedItems = new ArrayList();
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            // Set the dialog title
            builder.setTitle(R.string.pick_toppings)
                    // 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(R.array.toppings, 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

// in this case the OK button has more or less no function but if want save the check options in database or shared preference, you can put your code here. So it depends on your use case.

                        }
                    })
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                        }
                    });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    });

  }
}

我想我可以在父活动中创建一个变量
public static ArrayList checkedItems
,并简单地说
myActivity.checkedItems=mSelectedItems
,但这听起来……错了。我想我可以在父活动中创建一个变量
public static ArrayList checkedItems
,并简单地说
myActivity.checkedItems=mSelectedItems
但这听起来……错了。我自己的类中有我的
MultiChoiceDialog
,很抱歉没有提到这一点。我将在一秒钟内附加我实际项目代码的片段。如果该类是活动文件的内部类,您也可以使用与上述代码相同的方法,但如果它是,并且我是独立类,您可以在活动类中创建一个setter方法,并将数组作为参数传递。我对文档中的“listArray”感到非常困惑。我有一个包含复选框的布局。我需要响应该复选框。但是,如果我只是制作一个包含getString(R.string.show_password)的字符串列表来自android:text=“@string/show\u密码“在我的复选框中,对话框中有两个复选框。我不知道发生了什么。我的
多选对话
在自己的课堂上,很抱歉没有提到这一点。我将附加我实际项目代码的片段,一秒钟后如果该类是活动文件的内部类,您也可以使用与上面代码相同的方法,但如果它是独立类,您可以在活动类中创建setter方法并将数组作为参数传递。我对该文档中的“listArray”感到非常困惑。我有一个包含复选框的布局。我需要回应这个复选框。但是,如果我只是从android:text=“@string/show_password”复选框中创建一个包含getString(R.string.show_password)的字符串列表,我的对话框中会出现两个复选框。我不知道发生了什么事