当我使用带有listadapter的AlertDialog.Builder的setSingleChoiceItems时,单选按钮不显示

时间:2011-03-01 05:18:41

标签: android alertdialog listadapter

当我使用带有listadapter的AlertDialog.Builder的setSingleChoiceItems时,单选按钮不会显示。如果我使用Charsequence []数组,单选按钮会显示。请看一下我正在使用的代码。在此先感谢!!

公共类ExampleApp扩展了Activity {

Context mContext;
ListAdapter myListAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mContext = this;
    myListAdapter = new myListAdapter(mContext);
    AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
    alt_bld.setTitle("Select any icon");
    alt_bld.setSingleChoiceItems(myListAdapter, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), "Selected icon = "+imageName[item], Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog alert = alt_bld.create();
    alert.show();
}

private class myListAdapter extends BaseAdapter{
    public myListAdapter(Context mContext){
}

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imageName.length;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder myViewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listadapterview, null);
            myViewHolder = new ViewHolder();
            myViewHolder.txt_icon_name = (CheckedTextView) convertView.findViewById(R.id.CheckedTextView01);

            convertView.setTag(myViewHolder);
        }
        else{
            myViewHolder = (ViewHolder) convertView.getTag();
        }


        myViewHolder.txt_icon_name.setText(imageName[position]);
        return convertView;
    }

    class ViewHolder {
        CheckedTextView txt_icon_name;
    }
}

String[] imageName = {"icon1", "icon2", "icon3"};

} //用于listadapter视图的XML

2 个答案:

答案 0 :(得分:6)

我有类似的问题。即使我使用CheckedTextView,单选按钮也没有显示出来。

解决方案是将以下内容添加到布局文件中的CheckedTextView元素:

android:checkMark="?android:attr/listChoiceIndicatorSingle"

这是我的“dialog_list_item.xml”文件:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_list_item_title"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" 
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    >
</CheckedTextView>

以下是创建列表适配器的代码:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
     this.getContext(), R.layout.dialog_list_item, 
     cursor, 
     new String[]{"title"}, 
     new int[]{R.id.dialog_list_item_title}
);

我用谷歌搜索了它的答案,但我最终在sdk中找到了答案:

$android_sdk_home/platforms/android-x.x/data/res/layout/simple_list_item_single_choice.xml

有时源代码比任何谷歌搜索都要好。 ;)

答案 1 :(得分:1)

据推测,R.layout.listadapterview不是CheckedTextView,这是singleChoice工作所需的内容。