如何在警报对话框中将项目设置为选中?

How to set item as selected in alert dialog?

我正在显示用于选择列表的警告对话框。现在我想显示在警报对话框中选择的默认列表。我已将自定义适配器附加到列表中。我在对话框中使用了 setSingleChoiceItems。

为此,我已将第二个参数设置为 0,但我仍然看不到所选项目。

代码:

   ArrayList<ListData> allTables = new ArrayList<>();

        allTables = mListTableHelper.getAllList();

        final ListData taskList = new ListData();
        taskList.setId(100000000);
        taskList.setTitle("Default List");
        allTables.add(0, taskList);

        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddTaskActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View convertView = (View) inflater.inflate(R.layout.tablelist, null, false);

        ListView lv = (ListView) convertView.findViewById(R.id.tableslist);

        final ListItemAdapter adapter = new ListItemAdapter(AddTaskActivity.this,allTables);


        alertDialog.setSingleChoiceItems(adapter, 0 , new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog,int which) {


                ListData listOject = new ListData();

                listOject = listData.get(which);

                mListId = listOject.getId();

                //   listOject = mListTableHelper.getList(mListId);

                String title = listOject.getTitle();

                list.setText(listOject.getTitle());

                dialog.dismiss();

            }

        }).create();

        alertDialog.show();

ListItemAdapter

按照提示试了,还是选不到。

public class ListItemAdapter 扩展 ArrayAdapter{

int selectedItem;


    private static class ViewHolder {
        TextView title;
        RadioButton rb;
    }

    public ListItemAdapter(Context context, ArrayList<ListData> users,int selectedItem) {
        super(context, R.layout.list_item, users);
        this.selectedItem = selectedItem;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        ListData item = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.list_item, parent, false);
            viewHolder.title = (TextView) convertView.findViewById(R.id.list_item);
            viewHolder.rb = (RadioButton) convertView.findViewById(R.id.radioButton);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // Populate the data into the template view using the data object
        viewHolder.title.setText(item.getTitle());

        // Return the completed view to render on screen
        return convertView;
    }

}

适配器初始化

   final ListItemAdapter adapter = new ListItemAdapter(AddTaskActivity.this,allTables,0);

编辑:

删除了 .Create() 方法。它显示警告对话框初始化错误。

    public void showCustomList() {

        mAllLists = new ArrayList<>();

        mAllLists = mListTableHelper.getAllList();

        final ListData taskList = new ListData();
        taskList.setId(100000000);
        taskList.setTitle("Default List");
        mAllLists.add(0, taskList);


        alertDialog = new AlertDialog(AddTaskActivity.this);


        LayoutInflater inflater = getLayoutInflater();
        View convertView = (View) inflater.inflate(R.layout.tablelist, null, false);

        ListView lv = (ListView) convertView.findViewById(R.id.tableslist);

        final ListItemAdapter adapter = new ListItemAdapter(AddTaskActivity.this,mAllLists,selectedItem);


       alertDialog.setAdapter(adapter,null);

        alertDialog.show();

    }

这是怎么回事?谢谢..

在适配器的 getViewMethod 末尾 class 添加以下代码:

 viewHolder.rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked) {
                if(!isFirstTime) {
                    YouDialogHoldingClass.itemSelected(position);
                }
                else {
                   isFirstTime = false;
                }
                //selectedItem = position;
                //notifyDataSetChanged();
            }
        }
    });

    if(position == selectedItem) {
        isFirstTime = true;
        viewHolder.rb.setChecked(true);
    } else {
        viewHolder.rb.setChecked(false);
    }
    return convertView;

设置以下方法而不是设置 singleChoiceItems() 方法:

 alertDialog.setAdapter(adapter, null);

在显示对话框的 class 中创建一个静态方法:

 public static void itemSelected(int tempPos) {
            // do what you want on item selection....
    }

显示对话框如下:

 public void showCustomList() {

    mAllLists = new ArrayList<>();

    mAllLists = mListTableHelper.getAllList();

    final ListData taskList = new ListData();
    taskList.setId(100000000);
    taskList.setTitle("Default List");
    mAllLists.add(0, taskList);


    AlertDialog.Builder builder = new AlertDialog.Builder(AddTaskActivity.this);

    LayoutInflater inflater = getLayoutInflater();
    View convertView = (View) inflater.inflate(R.layout.tablelist, null, false);

    ListView lv = (ListView) convertView.findViewById(R.id.tableslist);

    final ListItemAdapter adapter = new ListItemAdapter(AddTaskActivity.this,mAllLists,selectedItem);

    builder.setAdapter(adapter, null);

    alertDialog = builder.create();

    alertDialog.show();

}