Wie verwende ich RadioGroup im benutzerdefinierten ListView-Adapter?

Möchte ich zeigen, eine einzelne select-option in meiner Liste. Ich bin mit RadioButton in meinem listView Zeile. Ich weiß, dass RadioGroup wird verwendet für Einzel-Auswahl.

Aber problem ist, ich habe die RadioButton in meinem ListRowView. Jetzt möchte ich hinzufügen, dass alle meine Elemente der Liste in einer RadioButton. Ich bin mit Custom Adapter und in getView(). Ich bekomme die RadioButton im getView(), aber wenn Sie möchten fügen Sie es in RadioGroup es sagen

"anzeigen bereits Eltern haben , rufen Sie removeView() in der Eltern vor der"

Und ich weiß, das ist wahr, aber wenn ich entfernen Sie Sie aus der Ansicht. Dann ist es nicht sichtbar.

Ich versuche auch, zu erstellen und fügen Sie RadioButton programmgesteuert. Und dann fügen Sie es in RadioGrop. Und dann zum anzeigen der Liste Zeile. Aber dieses mal, als die Eltern RadioGroup, so dass es wieder sagen

"anzeigen bereits Eltern haben , rufen Sie removeView() in der Eltern vor der"

Was ich tun möchte, ist wählen Sie nur ein Element in der Liste zu einem Zeitpunkt. Mein code ist wie folgt.

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 ;
    }

}

Meine XML für die Zeile

<?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>

Wie verwende ich RadioGroup im benutzerdefinierten ListView-Adapter?

Kommentar zu dem Problem
Wie Sie es lösen? Kommentarautor: Hardik Joshi

InformationsquelleAutor der Frage Arslan | 2011-09-07

Schreibe einen Kommentar