在Android中重新加载listview时,不会加载单选按钮

时间:2015-11-20 16:40:24

标签: android listview radio-button

我有一个包含自定义单元格的列表视图,如下所示,

<?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在初始加载期间加载正常,但是如果它滚动或新项目被列入列表,则RadioButton没有被加载。这是为列表视图的每个单元格加载视图的代码,

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

这里出了什么问题?

感谢。

1 个答案:

答案 0 :(得分:2)

你覆盖了radioButton id。

radioButton.setId(address.getAddressId());

RadioButton派生自View。我猜地址是你的域模型的一个对象。也许你想要:

radioButton.setChecked(true);