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

时间:2016-05-03 06:53:09

标签: java android android-alertdialog selecteditem

我正在显示一个警告对话框来选择列表。现在我想在警告对话框中显示所选的默认列表。我已将自定义适配器附加到列表中。我在对话框中使用了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

我按照建议尝试,仍然dint获取所选项目。

public class ListItemAdapter extends 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();

    }

这里有什么问题?谢谢..

1 个答案:

答案 0 :(得分:0)

在适配器类的getViewMethod结尾处添加以下代码:

 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);

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

 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();

}