android - 使用自定义适配器的 ListView 中的复选框刷新问题

标签 android

我有一个 ListView ,其中每个项目都由一组 TextView 和一个复选框组成。 我将复选框的状态存储在数据库中,并从 on clickListener 更新它。它适用于可见的控件。默认情况下,所有复选框都处于选中状态。

如果有 10 个项目并且屏幕可以容纳 7 个,那么当我取消选中第一个并滚动到第 10 个项目并再次滚动回第一个时。第一个失去了它的状态(它再次被检查)。我检查了数据库的行状态,它反射(reflect)正确。但是 BindView 中的获取总是让我处于错误的状态。我无法确定问题出在哪里。我附上了列表适配器以供审查...

//列表适配器代码

公共(public)类 ListAdaptor 扩展 CursorAdapter {

private LayoutInflater mInflater;
Cursor  dataCursor;
Context context;
ListView mLv;

private static final String TAG = "Delete";

public ListAdaptor(Context context, Cursor cursor, ListView lv) 
{
    super(context, cursor);
    this.context = context;
    mLv = lv;
    mInflater   = LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, final Cursor cursor) {

    // Get the stored tag for the view

    CheckBox tmp_Chk    = (CheckBox)view.findViewById(R.id.chkbox);
    String selText = cursor.getString(11); 

    // Debug Message
    int val = cursor.getPosition();

    tmp_Chk.setChecked(false);

    SparseBooleanArray sba = mLv.getCheckedItemPositions();
    if(sba != null)
    if(sba.get(cursor.getPosition()))
        tmp_Chk.setChecked(true);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) 
    {
        View newView = mInflater.inflate(R.layout.listviewlyt, null);
        return newView;
    }

}

// Item layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<CheckBox
    android:id="@+id/chkbox"
    android:focusable="false"
    android:clickable="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</CheckBox>


<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="18sp"
    android:textColor="#000000" >
</TextView>



</LinearLayout>

// List control code in the Main Activity

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    int lv_Pos = ListView.INVALID_POSITION;
    CheckBox tmp_Chk    = (CheckBox)view.findViewById(R.id.chkbox);
    if (lv_Pos != ListView.INVALID_POSITION) {
        if(tmp_Chk.isChecked()){
            Check_Uncheck(Integer.toString(lv_Pos + 1), 1);
    }
    else if(!tmp_Chk.isChecked()){
        Check_Uncheck(Integer.toString(lv_Pos + 1), 0);
    }   
}

public void Check_Uncheck(String deleteItem , int select)
{
    // Initialize database
    DB dbAdapters = DB.getDBAdapterInstance(TabActivity.this);
    dbAdapters.openDataBase();

    ContentValues cv_InitialValues = new ContentValues();
    cv_InitialValues.put("Selection", select);

    dbAdapters.b_UpdateRecordInDB("Items", cv_InitialValues, "_id=?", new String[] {deleteItem});
    dbAdapters.close();
}

});

//在主 Activity 中列出 View XML 属性

<ListView 
    android:id="@+id/LV_Instore_CartTab"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="2px"
    android:layout_weight="1"
    android:choiceMode="multipleChoice"/>

最佳答案

您将要覆盖 getView在你的适配器中。当滚动列表时 View 进入 View 时调用此函数。 convertView 变量是在您滚动时刚从 View 中消失并被重用的 View 。 convertView 可能为 null,因此您应该检查它是否为 null,如果它为 null,则扩充一个新的行布局,否则您可以像扩充它一样使用 convertView。现在发生在你身上的是 View 被重用,但你没有在 getView 函数中将状态设置回你想要的状态。在此函数中,您应该使用传入的位置变量来确定 View 正在连接列表中的哪个项目。如果将检查状态存储在对象列表中,则可以使用该位置从列表中获取正确的项目。使用您从列表中检索到的对象来选中或取消选中行中的复选框。

关于android - 使用自定义适配器的 ListView 中的复选框刷新问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8439435/

相关文章:

android - 如何在移动设备的 amp carousel 中显示导航箭头

android - Google Play 上是否还使用了特色图片和宣传图片?

android - java.util.ConcurrentModificationException?

android - 如何制作像 Bitspin 的 Timely Alarm Clock 这样的数字变形效果?

android - 如何给图片按钮添加点击效果?

java - 工具栏中的这两个图标(抽屉布局和菜单图标)是黑色的,如何将其变成白色?

java - JSON 输出未出现在 logcat 中

android - 高分辨率图像在 Android 上模糊,但在使用 Lightview 模态的 iPhone 上不模糊

android - 如何在 ListView 中的 EditText 中进行验证?

java - 如何检查文件名是否已经存在?