【发布时间】:2014-11-22 16:13:00
【问题描述】:
我有一个由具有特定布局的 CustomAdapter 填充的 ListView。
ArrayList 作为参数传递给此 CustomAdapter,其中包含每个 ListView 元素的数据。
每个 ListView 元素由一个 TextView(我在其中显示来自 ArrayList 的数据)和它右侧的一个 CheckBox 组成。
ArrayList 中的数据是正确的。当 ListView 被填充并且不在任何元素中重复时,此数据也会正确显示(有关详细信息,请参见下图)。
问题:当 ListView 被填充并显示在屏幕上时,当我检查一个 CheckBox 时,每 10 个元素下方也会检查另一个 CheckBox(有关详细信息,请参见下图)。
在这些图像中,您可以看到,当我检查第 1 个元素时,也检查了第 10 个元素。当我检查第 2 个元素时,第 11 个元素也被检查了!!!
即使我检查最后一个元素,也会检查第三个元素。
为什么会发生这种行为???
这是 CustomAdapter ElementContentAdapter.java 的代码:
@SuppressWarnings("rawtypes")
public class ElementContentAdapter extends BaseAdapter implements OnClickListener {
private final Activity activity;
private final ArrayList elements;
private static LayoutInflater layoutInflater = null;
public ElementContentAdapter(Activity activity, ArrayList data) {
this.activity = activity;
this.elements = data;
layoutInflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder {
public TextView tviContent;
}
@Override
public int getCount() {
if (elements.size() <= 0) return 1;
return elements.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public void onClick(View view) {
//
}
@SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView== null) {
convertView = layoutInflater.inflate(R.layout.element_content_checklist, null);
viewHolder = new ViewHolder();
viewHolder.tviContent = (TextView) convertView.findViewById(R.id.tvi_content_element_content_checklist);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (elements.size() <= 0) {
/**
*
*/
} else if(elements.size() > 0) {
ElementContent currentElement = null;
currentElement = (ElementContent) elements.get(position);
viewHolder.tviContent.setText(currentElement.getContent());
convertView.setOnClickListener(new ElementContentOnClickListener(position));
}
return convertView;
}
private class ElementContentOnClickListener implements OnClickListener {
private int selectedPosition;
ElementContentOnClickListener (int position) {
selectedPosition = position;
}
@Override
public void onClick(View view) {
CatMenusActivity catMenusActivity = (CatMenusActivity) activity;
catMenusActivity.elementoContentOnClick(selectedPosition);
}
}
}
这里是膨胀的布局代码element_content_checklist.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/White"
android:paddingBottom="10dp"
android:paddingRight="6dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/tvi_content_element_content_checklist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/cbo_mark_element_content_checklist"
android:gravity="left"
android:text="@string/txt_content_element_content"
android:textAllCaps="false"
android:textColor="@color/DimGray"
android:textSize="14sp"
android:textStyle="normal" />
<CheckBox
android:id="@+id/cbo_mark_element_content_checklist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:button="@drawable/content_bg_checkbox_content"
android:checked="false" />
</RelativeLayout>
ElementContent 类只是一个用于管理我的信息的自定义类,它可以从数据库中正确检索。它包含属性:elementId、contentId 和 content,以及各自的构造函数、getter 和 setter。
希望我能完全理解正在发生的事情以及我想要实现的目标。
非常感谢!
【问题讨论】:
-
这就是你的行回收。您必须使复选框成为视图持有者的一部分,然后根据标志对其进行检查,该标志可以为每个索引单独维护,也可以作为
ElementContent的一部分。点击复选框应该会改变这个标志,而不仅仅是视图。
标签: android listview android-listview arraylist