记住单选AlertDialog中选择的内容

时间:2013-10-11 21:00:37

标签: java android alertdialog

我有一个AlertDialog,它将数组显示在一个选定的选项中:

    protected boolean blFrom, blTo;
protected void showSelectToDialog() {
    boolean[] checkedDate = new boolean[toDate.length];
    int count = toDate.length;

    DialogInterface.OnClickListener setD2 = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //TODO Auto-generated method stub
            onChangeSelectedTo(which);
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select To Year");
    builder.setSingleChoiceItems(toDate, count, setD2);

    builder.setCancelable(true);
    builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    dialog2 = builder.create();
    dialog2.show();
}

protected void onChangeSelectedTo(int j) {
    bTo.setText(toDate[j]);
    sTo = ((AlertDialog)dialog2).getListView().getCheckedItemPosition();
    blTo = true;
    displayToast(String.valueOf(sTo));
    to = j;
    dialog.dismiss();
}

我想要做的是第一次加载它应该显示默认值。一旦我选择了一个选项并且对话框关闭并再次显示相同的对话框,它应该显示我之前选择的选项并滚动到它。

我如何完成它?

截至目前,我可以获得所选位置,但下一步是什么?

1 个答案:

答案 0 :(得分:2)

您可以将所选值存储在Activity的变量中或使用SharedPreferences