ListView每隔10个项目不规则地重复CheckBox行为

时间:2014-09-27 23:15:51

标签: android listview android-listview arraylist

我有一个由具有特定布局的CustomAdapter填充的ListView。
ArrayList作为参数传递给此CustomAdapter,其中包含每个ListView元素的数据 每个ListView元素由TextView(我在其中显示ArrayList中的数据)和右侧的CheckBox组成。

ArrayList中的数据是正确的。填充ListView时也会正确显示此数据,并且不会在任何元素中重复此数据(有关详细信息,请参阅下面的图像)。

问题:当ListView被填充并显示在屏幕上并且当我选中其中一个CheckBox时,每10个元素下面也会检查另一个CheckBox(详见下图)。 / p>

1st element checked 10th element checked

2nd element checked 11th element checked

Last element checked 3rd element checked

在这些图像中,您可以看到,当我检查第一个元素时,还会检查第10个元素。当我检查第二个元素时,也会检查第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只是一个用于管理我的信息的自定义类,可以从数据库中正确检索。它包含以下属性:elementIdcontentIdcontent及其各自的构造函数,getter和setter。

希望我完全了解正在发生的事情以及我想要实现的目标。

非常感谢提前!

1 个答案:

答案 0 :(得分:2)

将复选框添加到View Holder并定义getView()方法。 然后尝试使用SparseBooleanArray,您可以在单击项目时将其设置为true,并在刷新视图时进行检查以进行更新。

SparseBooleanArray pba=new SparseBooleanArray();//this should be global
private class ElementContentOnClickListener implements OnClickListener {

    private int selectedPosition;

    ElementContentOnClickListener (int position) {
        selectedPosition = position;
        pba[position]=true;

    }

    @Override
    public void onClick(View view) {
        CatMenusActivity catMenusActivity = (CatMenusActivity) activity;
        catMenusActivity.elementoContentOnClick(selectedPosition);
    }

}

然后在您的getView()方法中...手动检查所选位置并相应地更新复选框

holder.checkBox.setChecked(pba[position]); // inside getView()
相关问题