Zach
Zach

Reputation: 10119

Radio button is not loaded when listview reloads in Android

I have a list view containing a custom cell as given below,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:layout_height="150dp">


    <RadioButton
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".10"
        android:id="@+id/radioButtonSelectAddress"
        android:layout_marginLeft="5dp"
        android:enabled="false"
        android:layout_gravity="center_vertical" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.80"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/txt_Street"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="15dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/txt_Locality"
            android:layout_below="@+id/txt_Street"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="5dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/txt_City"
            android:layout_below="@+id/txt_Locality"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="5dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/txt_PostCode"
            android:layout_below="@+id/txt_City"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="5dp"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".10">

        <ImageButton
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:scaleType="fitXY"
            android:id="@+id/editButton"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/edit"
            android:background="@null"/>
    </RelativeLayout>
</LinearLayout>

Listview loads fine during the initial load, but if its scrolled or a new item gets aded to list the RadioButton is not getting loaded.Here is the code to load view for each cell of list view,

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.address_list_item, null);
            }
            RadioButton radioButton = (RadioButton)convertView.findViewById(R.id.radioButtonSelectAddress);
            TextView txt_Street = (TextView)convertView.findViewById(R.id.txt_Street);
            TextView txt_Locality = (TextView)convertView.findViewById(R.id.txt_Locality);
            TextView txt_City = (TextView)convertView.findViewById(R.id.txt_City);
            TextView txt_PostCode = (TextView)convertView.findViewById(R.id.txt_PostCode);
            ImageButton editButton = (ImageButton)convertView.findViewById(R.id.editButton);
            Address address = realmResults.get(position);
            radioButton.setId(address.getAddressId());
            txt_Street.setText(address.getHouseNumber() + " "+address.getStreet());
            txt_Locality.setText(address.getLocality());
            txt_City.setText(address.getCity());
            txt_PostCode.setText(address.getPostCode());
            return convertView;
        }

Whats going wrong here?

Thanks.

Upvotes: 0

Views: 33

Answers (1)

JanPl
JanPl

Reputation: 804

You overwrite the radioButton id.

radioButton.setId(address.getAddressId());

RadioButton is derived from View. I guess address is an object of your domain model. Maybe you want:

radioButton.setChecked(true);

Upvotes: 2

Related Questions