【问题标题】:Check box refresh issue in ListView with custom adaptor使用自定义适配器的 ListView 中的复选框刷新问题
【发布时间】:2011-12-09 00:03:28
【问题描述】:

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

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

// 列出适配器代码

公共类 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();
}

});

// 主活动中的列表视图 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"/>

【问题讨论】:

  • 我已经在这里回答了“stackoverflow.com/a/8410557/525978”如果这没有帮助..让我知道...我可以检查您的代码...
  • 一个简单的问题,其他值是否正确检索?还是只是复选框给出了问题?
  • @Vamsi,其他值 r 已正确检索。
  • @havexz / Bobakke4 我有一个疑问,BindView 和 Getview 有什么区别。我看到你们的建议已经在 BindView 方法中完成了。感谢您的帮助...
  • 两者都可以正常工作。如果使用 CursorAdapter,则为 bindView;如果使用 ListAdapter 或 BaseAdapter,则为 getView

标签: android


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我不是很了解CompundButton,但是你可以试试下面的代码sn-p,

    tmp_Chk.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if ( !tmp_Chk.isChecked()) {
                    tmp_Chk.setChecked(false);  
                    Check_Uncheck();
                } else {
                    Check_Uncheck();
                    tmp_Chk.setChecked(true);
    
                }
            }
        });
    
        if (cursor.getInt(11) == 0) {
            tmp_Chk.setChecked(false);
        } else {
            tmp_Chk.setChecked(true);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      相关资源
      最近更新 更多