用于AlertDialog SingleChoiceItems的Android SQLite自定义ListAdapter

时间:2014-03-05 08:04:07

标签: android sqlite alertdialog listadapter

我想创建一个Dialog,用户可以选择一个单选项并单击“确定”。列表项必须包含来自sqlite db的id和文本(但只显示给用户的文本)。由于setSingleChoiceItems接受ListAdapter,我可能需要创建一个自定义List适配器并从我的db select结果中加载项目。我实际上需要帮助来创建这个自定义ListAdapter。只有在用户点击“确定”后,我才需要获取所选项目文本和ID以保存到相应的SharePreferences字段中。

1 个答案:

答案 0 :(得分:1)

好的,我设法设置了我的自定义列表适配器,但不幸的是这些项目没有显示为无线电,这是我的代码:

    public void changePocket(View view)
{   
    ListAdapter liAdabter = new ListAdapter() {

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public int getViewTypeCount() {

            return 1;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            PocketsObj pocketListItems = pocketsList.get(position);

            if (convertView == null) {

                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(layoutResourceID, parent, false); 
            }

            TextView idText = (TextView) row.findViewById(R.id.pocket_id);
            if (idText != null) {
                idText.setText(String.valueOf(pocketListItems._pocket_id));
            }
            TextView nameText = (TextView) row.findViewById(R.id.pocket_name);
            if (nameText != null) {
                nameText.setText(pocketListItems._pocket_name);
            } 
            return row;
        }

        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public Object getItem(int position) {

            return pocketsList.get(position);
        }

        @Override
        public int getCount() {

            return pocketsList.size();
        }

        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean areAllItemsEnabled() {
            // TODO Auto-generated method stub
            return false;
        }
    };

     AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this, 2);            
     alertBuilder.setTitle(R.string.change_pockets)
                .setSingleChoiceItems(liAdabter, 1,
                  new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                        }
                   })
                .setPositiveButton(R.string.change_pockets_ok, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton(R.string.change_pockets_cancel, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        return;                         
                    }
                });

    alertBuilder.show();
}

        <TextView
        android:id="@+id/pocket_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible" >
    </TextView>

    <TextView
        android:id="@+id/pocket_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:layout_margin="5dp" >
    </TextView>