自定义列表视图的警报对话框

时间:2016-03-06 08:09:03

标签: java android listview android-studio android-alertdialog

我想创建一个包含自定义列表视图的警告对话框。为此,我为列表视图创建了自定义布局和适配器。 我想要单选项目的警报对话框。所以我在对话框中设置了一个适配器。现在我想从数组列表中获取所选项目?

  selectTable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final TimeTable table = new TimeTable();

                List<TimeTable> allTables = new ArrayList<TimeTable>();

                TimeTableHelper timeTableHelper = new TimeTableHelper(AddEventActivity.this);

                allTables = timeTableHelper.getAllTables();



                allTables.add(table);

                final List<TimeTable> t = th.getAllTables();
                for (TimeTable cn : allTables) {
                    String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() +
                            "Status: " + cn.getStatus() + ",Color: " + cn.getTableColor();

                    Log.d("Data ", log);
                }
                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddEventActivity.this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = (View) inflater.inflate(R.layout.tablelist, null, false);

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

                final CustomAlertAdapter adapter = new CustomAlertAdapter(allTables, AddEventActivity.this);


                alertDialog.setView(convertView);

                                   alertDialog.setView(customView);

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


                       final List<TimeTable> tables = new ArrayList<TimeTable>();

                       // what to do here to get item of TimeTable list?
                   }
               });

                alertDialog.show();
            }
        });

编辑:

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


                   TimeTable selectedItem = allTables.get(which);

                   selectedItem.getId();
                   txtTable.setText(selectedItem.getTitle());
                   Log.d("TableId", String.valueOf(selectedItem));

                   dialog.dismiss();

               }
           });

            alertDialog.show();
        }

customAlertAdapter

public class CustomAlertAdapter extends BaseAdapter{


        List<TimeTable> mData;
        Context mContext;
        LayoutInflater inflater;
        public CustomAlertAdapter(List<TimeTable> data, Context context) {
            mData = data;
            mContext = context;
            inflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null ) {

                LayoutInflater mInflater = (LayoutInflater) mContext
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

                convertView = mInflater.inflate(R.layout.alertlistrow, null);

            }

            TextView tvTitle = (TextView) convertView.findViewById(R.id.tableTitle);
            Button color = (Button)convertView.findViewById(R.id.selectColor);


            tvTitle.setText(mData.get(position).getTitle());

            ((GradientDrawable)color.getBackground()).setColor(mData.get(position).getTableColor());

            return convertView;
        }

}

1 个答案:

答案 0 :(得分:2)

which参数表示所选项目的索引。所以你可以写:

TimeTable selectedItem = allTables.get(which);

此外,我认为当您调用setSingleChoiceItem方法时,无需为对话框提供自定义视图。

以下是一个包​​含字符串数组的工作示例:

AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle("Dialog").setSingleChoiceItems(new String[]{"Item 1", "Item 2"}, 0, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0:
                            //Item 1 was selected
                            break;
                        case 1:
                           //Item 2 was selected
                            break;
                    }
                    dialog.dismiss();
                }
            }).create();
dialog.show();

您可以传递ListAdapter而不是String数组。