4

I'm trying to inflate my layout for a Listview with a row that has two TextViews and a RadioButton. I want all the RadioButtons added this way to be in the same RadioGroup. Here's what I've done so far:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {        
            LayoutInflater vi = 
                (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.config_grid_item, null);
            holder = new ViewHolder();
            holder.item1 = (TextView) v.findViewById(R.id.config_item1);
            holder.item2 = (TextView) v.findViewById(R.id.config_item2);
            holder.item3 = (RadioButton) v.findViewById(R.id.config_item3);
            group.addView(holder.item3);
            v.setTag(holder);
        }
        else
            holder=(ViewHolder)v.getTag();

        final Profile profile = profiles.get(position);
        if (profile != null) {
            holder.item1.setText(profile.IP);
            holder.item2.setText(profile.name);
        }
        return v;
    }

And the row XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:minHeight="60dip">

    <TextView android:id="@+id/config_item2" android:text="Name"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_gravity="center_vertical" android:width="130dip"
        android:padding="10dip" />

    <TextView android:id="@+id/config_item1" android:text="IP"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_gravity="center_vertical" android:width="130dip"
        android:padding="10dip" />
    <RadioButton android:id="@+id/config_item3" android:width="40dip"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:checked="false" android:focusable="false"
        android:focusableInTouchMode="false" />
</LinearLayout>

It crashes at group.addView(holder.item3). The error log is here:

08-15 13:37:21.178: ERROR/AndroidRuntime(19749): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1993)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.ViewGroup.addView(ViewGroup.java:1888)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.widget.RadioGroup.addView(RadioGroup.java:139)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.ViewGroup.addView(ViewGroup.java:1845)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.ViewGroup.addView(ViewGroup.java:1825)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at com.bluerisc.main.ConfigRowAdapter.getView(ConfigRowAdapter.java:67)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.widget.AbsListView.obtainView(AbsListView.java:1357)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1200)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.widget.ListView.onMeasure(ListView.java:1111)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.View.measure(View.java:8222)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3155)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.View.measure(View.java:8222)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3155)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.View.measure(View.java:8222)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3155)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.View.measure(View.java:8222)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3155)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.View.measure(View.java:8222)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.ViewRoot.performTraversals(ViewRoot.java:805)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1764)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.os.Looper.loop(Looper.java:143)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at android.app.ActivityThread.main(ActivityThread.java:5068)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at java.lang.reflect.Method.invokeNative(Native Method)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at java.lang.reflect.Method.invoke(Method.java:521)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-15 13:37:21.178: ERROR/AndroidRuntime(19749):     at dalvik.system.NativeStart.main(Native Method)

From the error log, I can guess that because the RadioButton is already a child of a LinearLayout in the row xml that it doesn't want to be added to the radioGroup. Any idea on how to fix this? I basically just want one of the radiobuttons selected at a time which is why I'm adding them to a radiogroup.

edit: I just got the genius idea of listening to the error message and trying to remove the view from the LinearLayout. It doesn't crash, but now the RadioButton doesn't appear in my row. Slight progress? Here's what I added/changed:

        LinearLayout test = (LinearLayout) v.findViewById(R.id.LinearRadio);
        test.removeView(holder.item3);
        group.addView(holder.item3);
telkins
  • 10,440
  • 8
  • 52
  • 79

3 Answers3

0

You cannot add a view to 2 layouts at the same time.
After reading your edits: Yes you will not be able to view that radio button since its been removed from its visible parent and added to an invisible radiogroup.

It seems you need a listview with singlechoice with two lines in each row. listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); with item row as simple_list_item_2 will do the need full.

Ron
  • 24,175
  • 8
  • 56
  • 97
  • I'm confused. Change my current ListView to this? Or add a new ListView inside of my LinearLayout to then add to my ListView? Also, why do I need two rows? – telkins Aug 15 '11 at 18:55
  • your requirement is name field and corresponding IP in each row. So using two line item list view will look better than putting both in a single line. Also u will get more width for the name field. Think about it. Change your current listview. Set its choice mode to single and pass the simple_list_item_2 as the resource to adapter constructor. – Ron Aug 16 '11 at 03:11
  • The setChoiceMode didn't work. :\ I think the radiobuttons still need to be in a radiogroup no matter what list type. Also, the layout is how I want it. More profiles can fit on the screen having the rows thinner. – telkins Aug 16 '11 at 13:34
0

You need to add the parentless LinearLayout to the RadioGroup.

        LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.config_grid_item, null);
        holder = new ViewHolder();
        holder.item1 = (TextView) v.findViewById(R.id.config_item1);
        holder.item2 = (TextView) v.findViewById(R.id.config_item2);
        holder.item3 = (RadioButton) v.findViewById(R.id.config_item3);
        group.addView(v);
Graeme
  • 25,714
  • 24
  • 124
  • 186
-1

not good solution...just uncheck all radiobuttons . (Radiogroup is not necessary at all) private void uncheckRadioBtn() {for (int i = 0; i < contactMaxNum; ++i)if (rb[i].isChecked())rb[i].setChecked(false);}