Я не уверен, что это только я или что-то еще не так в моем коде.Я заполняю все телефонные контакты в пользовательском списке, и все нормально, пока не заполнится.У настраиваемого списка есть флажок, чтобы можно было выбрать несколько элементов.Проблема заключается в том, что в списке просмотра загружено 30 контактов, и я выбираю первый контакт и прокручиваю его вниз. Я вижу, что 11-й и 21-й контакты также выбраны.Я не уверен, почему это происходит, любая информация в этой необычной проблеме будет полезна.
Вот код xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
android:choiceMode="multipleChoice"/>
<TextView android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/main_no_contacts"
android:textColor="@color/white"/>
</LinearLayout>
Настраиваемый макет
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlContactListRowMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<ImageView android:id="@+id/ivContactIconRow"
android:src="@drawable/droid_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RelativeLayout android:id="@+id/rlContactListRowChild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ivContactIconRow"
android:layout_alignTop="@+id/ivContactIconRow">
<TextView android:text="Vikram Ramanathan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvRowContactName"
android:paddingLeft="10dip"
android:textColor="@color/white"/>
<TextView android:text="Phone Numbers: "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:id="@+id/tvRowContactPhone"
android:layout_below="@+id/tvRowContactName"
android:textColor="@color/white"/>
</RelativeLayout>
<CheckBox android:id="@+id/chkSelectContact"
android:layout_height="wrap_content"
android:text=""
android:layout_width="wrap_content"
android:layout_alignParentRight="true"></CheckBox>
</RelativeLayout>
Код Java
super.onCreate(savedInstanceState);
setContentView(R.layout.listcontacts);
phoneContactList = new ArrayList<PhoneContactInfo>();
this.m_adapter = new PhoneContactListAdapter(this, R.layout.listcontacts_row, phoneContactList);
setListAdapter(this.m_adapter);
Код адаптера
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listcontacts_row, null);
}
PhoneContactInfo phoneContactInfo = phoneContacts.get(position);
if (phoneContactInfo != null)
{
TextView tvContactName = (TextView) v.findViewById(R.id.tvRowContactName);
if (tvContactName != null)
{
tvContactName.setText(phoneContactInfo.getContactName());
}
TextView tvContactNumber = (TextView) v.findViewById(R.id.tvRowContactPhone);
if (tvContactNumber != null)
{
tvContactNumber.setText(phoneContactInfo.getContactNumber());
}
}
v.setTag(phoneContactInfo);
phoneContactInfo = null;
return v;