在警报对话框中实现自定义列表视图

时间:2016-03-02 16:49:34

标签: java android listview android-alertdialog custom-lists

我正在警报对话框中实现自定义列表视图。添加到自定义适配器时,它在列表中显示错误,java.util.ArrayList无法应用于java.util.list。

我创建了一个查询来获取表格中的标题列表。我想在警告对话框中显示此列表。

出了什么问题?

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

            int selected = 0;

            TimeTableHelper th = new TimeTableHelper(getApplicationContext());
            final List<String> tables = new ArrayList<String>(th.getTitle());

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddEventActivity.this);
            LayoutInflater inflater = getLayoutInflater();
            View convertView = (View) inflater.inflate(R.layout.alertlistrow, null);
            alertDialog.setView(convertView);
            ListView lv = (ListView) convertView.findViewById(R.id.listView1);

            adapter=new CustomAlertAdapter(AddEventActivity.this, tables);
            lv.setAdapter(adapter);

            alertDialog.setSingleChoiceItems(adapter,selected,
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int item) {

                            ListView lw = ((AlertDialog)dialog).getListView();
                            Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());

                            txtTable.setText(String.valueOf(checkedItem));
                            dialog.dismiss();
                        }
                    });
            alertDialog.show();

        }
    });

CustomAlertAdapter

public class CustomAlertAdapter extends BaseAdapter{

        Context ctx=null;
        ArrayList<String> listarray=null;
        private LayoutInflater mInflater=null;
        public CustomAlertAdapter(Activity activty,ArrayList<String> list)
        {
            this.ctx=activty;
            mInflater = activty.getLayoutInflater();
            this.listarray=list;
        }
        @Override
        public int getCount() {

            return listarray.size();
        }

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

        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup arg2) {
            final ViewHolder holder;
            if (convertView == null ) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.alertlistrow, null);
                holder.titlename = (TextView) convertView.findViewById(R.id.textViewTitle);
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }

            String datavalue=listarray.get(position);
            holder.titlename.setText(datavalue);

            return convertView;
        }

        private static class ViewHolder {
            TextView titlename;
        }

}

查询:

public ArrayList<String> getTitle() {

    ArrayList<String> conList = new ArrayList<String>();


    String selectQuery = "SELECT  * FROM " + TABLE_TIME_TABLE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {

            TimeTable table = new TimeTable();

          //  table.setId(Integer.parseInt(cursor.getString(0)));
            table.setTitle(cursor.getString(1));
            conList.add(table.getTitle());
        } while (cursor.moveToNext());
    }

    return conList;
}

谢谢。

1 个答案:

答案 0 :(得分:0)

您的CustomAlertAdapter在其构造函数中占用ArrayList<String>。用线

final List<String> tables = new ArrayList<String>(th.getTitle());

您将tables声明为List<String>。它实际上是作为ArrayList<String>创建的,但那是Java - tables只是一种List,因此以下行不起作用:

adapter=new CustomAlertAdapter(AddEventActivity.this, tables);

你能做什么?

首先将tables声明为ArrayList<String>或者像这样调用适配器构造函数:

adapter=new CustomAlertAdapter(AddEventActivity.this, (ArrayList<String>)tables);