繁体   English   中英

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

[英]Implementing custom listview in alert dialog

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

我创建了一个查询以从表中获取标题列表。 我想在警报对话框中显示此列表。

怎么了

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

谢谢。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM