Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于AlertDialog SingleChoiceItems的Android SQLite自定义ListAdapter_Android_Sqlite_Android Alertdialog_Listadapter - Fatal编程技术网

用于AlertDialog SingleChoiceItems的Android SQLite自定义ListAdapter

用于AlertDialog SingleChoiceItems的Android SQLite自定义ListAdapter,android,sqlite,android-alertdialog,listadapter,Android,Sqlite,Android Alertdialog,Listadapter,我想创建一个对话框,用户可以在其中选择一个单选选项并单击“确定”。列表项必须同时包含来自sqlite db的id和文本(但仅向用户显示文本)。由于setSingleChoiceItems接受ListAdapter,我可能需要创建一个自定义列表适配器并从我的db select结果中加载项。我实际上需要帮助来创建这个自定义ListAdapter。只有在用户单击“确定”后,我才需要获取所选项目文本和id以保存到相应的SharePreferences字段中 好的,我设法设置了自定义列表适配器,但不幸的是

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

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

    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>
public void changePocket(视图)
{   
ListAdapter liadapter=新ListAdapter(){
@凌驾
public void unregisteredDataSetobserver(DataSetObserver观察员){
//TODO自动生成的方法存根
}
@凌驾
公共无效注册表DataSetObserver(DataSetObserver observer){
//TODO自动生成的方法存根
}
@凌驾
公共布尔值为空(){
//TODO自动生成的方法存根
返回false;
}
@凌驾
公共布尔表ID(){
//TODO自动生成的方法存根
返回false;
}
@凌驾
public int getViewTypeCount(){
返回1;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
PocketsObj pocketListItems=pocketsList.get(位置);
if(convertView==null){
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceID,父级,false);
}
TextView id text=(TextView)row.findViewById(R.id.pocket\u id);
if(idText!=null){
idText.setText(String.valueOf(pocketListItems.\u pocket\u id));
}
TextView name text=(TextView)row.findViewById(R.id.pocket\u name);
如果(nameText!=null){
nameText.setText(pocketListItems.\u pocket\u name);
} 
返回行;
}
@凌驾
public int getItemViewType(int位置){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共对象getItem(int位置){
返回口袋列表。获取(位置);
}
@凌驾
public int getCount(){
返回pocketsList.size();
}
@凌驾
公共布尔值isEnabled(整型位置){
//TODO自动生成的方法存根
返回false;
}
@凌驾
公共布尔值areAllItemsEnabled(){
//TODO自动生成的方法存根
返回false;
}
};
AlertDialog.Builder alertBuilder=新建AlertDialog.Builder(此,2);
alertBuilder.setTitle(R.string.change_pocket)
.setSingleChoiceItems(责任人,1,
新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//TODO自动生成的方法存根
}
})
.setPositiveButton(R.string.change\u pocket\u ok,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
}
})
.setNegativeButton(R.string.change\u pocket\u cancel,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
返回;
}
});
alertBuilder.show();
}

您为此尝试过什么吗?我尝试过声明一个ListAdapter,它被作为setSingleChoiceItems上的参数接受,以完成适配器,尽管我需要帮助来更新这些方法:public View getView(int position,View convertView,ViewGroup parent),public long getItemId(int position)以及其他生成ListAdpter对象的方法。