ListView的重复复选框行为每10项不正常 [英] ListView repeated CheckBox behavior every 10th item erratically

查看:185
本文介绍了ListView的重复复选框行为每10项不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由CustomAdapter了特定布局填补一个ListView。结果
一个ArrayList作为参数传递给这个CustomAdapter,其中包含每个元素的ListView中的数据通过。结果
每个ListView的元素由一个TextView(这里我显示来自ArrayList中的数据),并在它的右边一个复选框组成。

I have a ListView that is filled by a CustomAdapter with a specific Layout.
An ArrayList is passed as a parameter to this CustomAdapter, which contains the data for each ListView element.
Each ListView element is composed by a TextView (where I display the data from the ArrayList) and a CheckBox at the right of it.

在ArrayList中的数据是正确的。这个数据时,在ListView充满还显示正确,它不是任何元素重复(参见下面的图像细节)。

The data in the ArrayList is correct. This data is also correctly shown when the ListView is filled and it's not repeated in any element (see images below for details).

问题::当ListView的填充并显示在屏幕上,当我检查复选框中的一个,另一个复选框还检查每10元以下(参阅下图了解详细信息)

The issue: When the ListView is filled and displayed on the screen and when I check one of the CheckBoxes, another CheckBox is also checked below every 10th element (see images below for details).




在这些图片你可以看到,当我检查了1个元素,第10单元也被选中。当我检查了第二个元素,第11单元也被检查过!结果
即使我检查的最后一个元素,第三个元素被选中。

In these images you can see, when I check the 1st element, the 10th element is also checked. When I check the 2nd element, the 11th element is also checked too!!!
Even if I check the last element, the 3rd element is checked.

为什么这种行为发生???

下面是code为CustomAdapter ElementContentAdapter.java

Here is the code for the 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);
    }

}
}

下面是布局的code被夸大的 element_content_checklist.xml

Here is the code for the Layout that is inflated 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 内容识别内容,与其各自的构造函数,getter和setter方法​​。

The class ElementContent is just a custom class for managing my information, wich is retrieved correctly from a database. It contains the attributes: elementId, contentId and content, with its respective constructor, getters and setters.

希望我能完全理解正在发生的事情,我想达到什么

Hope I get fully understood for what is happening and what I want to achieve.

提前感谢!

推荐答案

添加复选框到View持有人,并把它定义 getView()方法。
然后尝试使用SparseBooleanArray在其中您可以将其设置为true时,单击的项目,可以检查,同时全新看法进行更新。

Add Checkbox to your View Holder and define it getView() method. Then try Using SparseBooleanArray in which you can set it to true when item clicked and can check while refreshing view to update.

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() Method..manually检查选定的位置和更新相应的复选框。

then in your getView() Method..manually check for selected positions and update checkbox accordingly

holder.checkBox.setChecked(pba[position]); // inside getView()

这篇关于ListView的重复复选框行为每10项不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆