如何在ListView中自定义适配器使用RadioGroup中?自定义、适配器、如何在、RadioGroup

2023-09-12 00:29:29 作者:醉酒

我想告诉我的列表中只有一个选择选项。我在我的ListView行使用单选按钮。我知道,RadioGroup中用于单项选择。

I want to show a single select option in my list. I am using RadioButton in my listView row. I know that RadioGroup is used for single selection.

但问题是,我在ListRowView添加的单选按钮。现在,我想在一个单选按钮添加我的所有列表项。我使用自定义的适配器和getView()。如果想将其添加在RadioGroup中也说,我得到getView的单选按钮()对接

But Problem is that I have added the Radio button in my ListRowView. Now I want add all my list items in one RadioButton. I am using custom Adapter and in getView(). I get the radiobutton in getView() butt when want to add it in RadioGroup it say

查看媒体链接有父,叫removeView()的母公司之前

"view allready have parent , call removeView() in parent before"

和我知道这是真的,但如果我从视图中删除。那么它是不可见的。

And I know its true, but If I remove it from the view. then it is not visible.

我也尝试创建并添加单选按钮编程。然后在RadioGrop添加它。然后,查看列表行的。但这次作为父是RadioGroup中如此反复好说

I also try to create and add RadioButton programatically. And then add it in RadioGrop. And then to view of list row. But this time as the parent is RadioGroup so again it say

查看媒体链接有父,叫removeView()的母公司之前

"view allready have parent , call removeView() in parent before"

我想要做的就是在一个时间只选择一个在列表项。我的code是如下:

What I want to do is to select only one item in list at a time. My code is as follows.

 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的行

My XML for the row

<?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); 请您自定义行认为实施可检查。 (关于这个更多的信息在这里)。 Use mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); Make your custom row view implement Checkable. (More info about this here).