1

I'm trying to create an AlertDialog with an ArrayAdapter to show a list of items for the user to select form (single choice). However, I would like to show which item was previously selected. Since selecting an item automatically closes the dialog, my thought was to set a custom background color for the previously selected item. Below is a mockup of what I'm hoping to achieve:

enter image description here

The code I have currently is this:

private void showCustomAlert(int alertNumber, int parentTaskId) {
    Log.w(LOGTAG, showCustomAlert);         
    int alertNumberID = Integer.parseInt(task1_id);
    if (alertNumber==2) alertNumberID = Integer.parseInt(task2_id);
    else if (alertNumber==3) alertNumberID = Integer.parseInt(task3_id);
    else if (alertNumber==4) alertNumberID = Integer.parseInt(task4_id);

    AlertDialog.Builder builderSingle = new AlertDialog.Builder(thisActivity);
    builderSingle.setIcon(R.drawable.icon);
    builderSingle.setTitle("Select Task:");


    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(thisActivity,
            android.R.layout.select_dialog_singlechoice);
    for (int i=0; i<taskList.size(); i++) {
        BT_item tmpItem = taskList.get(i);
        if (BT_strings.getJsonPropertyValue(tmpItem.getJsonObject(),"task_parent","").equalsIgnoreCase(parentTaskId + ""))
            arrayAdapter.add(BT_strings.getJsonPropertyValue(tmpItem.getJsonObject(),"task_name",""));
    }

    builderSingle.setNegativeButton("cancel",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

    builderSingle.setAdapter(arrayAdapter,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int taskSelected) {
                    Log.w(LOGTAG, "task selected"); 

                }
            });
    builderSingle.show();

}
4

1 回答 1

2

试试这个..而不是你的 builderSingle.setAdapter 使用下面的行

                builderSingle
                    .setCancelable(true)
                    .setSingleChoiceItems(display_array, selected_pos, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog1, int pos) {
                            // TODO Auto-generated method stub

                            selected_pos = pos; 

                            dialog.cancel();
                        }
                    })

display_array 是您要在数组中的对话框中显示的项目..

于 2013-10-19T16:05:05.360 回答