自定义列表视图和适配器以及列表视图选择变得疯狂



我不确定是否只是我或其他东西是错误的在我的代码。我填充所有的电话联系人在一个自定义列表视图,一切都很好,直到它的填充。自定义列表视图有一个复选框,因此可以选择多个项目。问题是,当我在listview中加载了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;

我认为问题是,当你填充项目你更新TextViews和你不更新CheckBox。您的布局列表项被重用,这就是为什么下一个项目有CheckBox复选框。尝试为列表或自定义类中的每个保存CheckBox状态,并从那里保存或加载它。

我认为Mighter是对的。您可以通过删除if (v == null)检查并每次从头重新创建视图来快速确认这一点。当你使用循环视图,你将(即convertView),你应该确保重置所有的视图在适当的。

根据此列表的性能水平,您可能还希望使用ViewHolder模式,而不是在每次调用getView()时调用findViewById。我推荐这个来自Google I/O 2010的演讲,它详细介绍了listview和适配器:http://www.youtube.com/watch?v=wDBM6wVEO70

最新更新