我想在我的列表中显示一个选择选项。我在 listView
行中使用 RadioButton
。我知道 RadioGroup
用于单选。
但问题是我在 ListRowView
中添加了 RadioButton
。现在我想将我的所有列表项添加到一个 RadioButton
中。我正在使用 Custom Adapter
和 getView()
。我在 getView()
中得到了 RadioButton
,但是当想在 RadioGroup
中添加它时,它说
"view already have parent , call removeView() in parent before"
而且我知道它是真的,但如果我将它从 View 中删除。然后就看不到了。
我还尝试以编程方式创建和添加 RadioButton
。然后将它添加到 RadioGrop
中。然后查看列表行。但是这次作为父级是 RadioGroup
,所以再说一遍
"view already have parent , call removeView() in parent before"
我想要做的是一次只选择列表中的一项。我的代码如下。
getView
public class MyAdapter extends ArrayAdapter < MyMenuItem > {
private LayoutInflater mInflater ;
int mResource ;
List < MyMenuItem > mData ;
Context context;
public MyAdapter ( Context context , int resource , int textViewResourceId , List < MyMenuItem > data ) {
super ( context , resource , textViewResourceId , data ) ;
this.context = context;
mData = data ;
mResource = resource ;
mInflater = ( LayoutInflater ) getSystemService ( Context.LAYOUT_INFLATER_SERVICE ) ;
}
@ Override
public View getView ( int position , View convertView , ViewGroup parent ) {
ViewHolder holder = null ;
if ( convertView == null ) {
convertView = mInflater.inflate ( mResource , null ) ;
holder = new ViewHolder ( ) ;
holder.icon = ( ImageView ) convertView.findViewById ( R.id.icon ) ;
holder.text = ( TextView ) convertView.findViewById ( R.id.text ) ;
holder.comment = ( TextView ) convertView.findViewById ( R.id.comment ) ;
LinearLayout lin = ( LinearLayout ) convertView.findViewById ( R.id.linerList ) ;
RadioButton rbtn = new RadioButton ( context );
LayoutParams lparam = new LayoutParams ( LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT );
rbtn.setSelected ( false );
holder.check = rbtn;
//radioGroup.addView ( rbtn );
lin.addView ( rbtn , 0 );
convertView.setTag ( holder ) ;
} else {
holder = ( ViewHolder ) convertView.getTag ( ) ;
}
holder.text.setText ( mData.get ( position ).getText ( ) ) ;
holder.comment.setText ( mData.get ( position ).getComment ( ) ) ;
holder.icon.setImageResource ( getApplicationContext ( ).getResources ( ).getIdentifier ( mData.get ( position ).getIcon ( ) ,
"drawable" , getPackageName ( ) )
) ;
return convertView ;
}
}
我的行的 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<LinearLayout
android:id = "@+id/linerList"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="My Application"
android:textSize="20sp"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="@color/white" />
<TextView
android:id="@+id/comment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="14sp"
android:textColor="@color/light_gray" />
</LinearLayout>
最佳答案
你需要做两件事:
- 使用
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
- 使您的自定义行 View 实现
Checkable
。 (有关此 here 的更多信息)。
关于android - 如何在 ListView 自定义适配器中使用 RadioGroup?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7329856/