当前位置: 首页 > 知识库问答 >
问题:

当视图滚动时,Android ListView失去复选框状态[重复]

张照
2023-03-14

我已经设法让我的应用程序在复选框状态更改时更新数据库,但当我滚动应用程序时,复选框的状态会更改。

我的问题是:如何保存复选框的状态,以便在滚动视图时它们不会更改?

这是我的光标适配器。

public CustomCursorAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
     mInflater = (LayoutInflater) 
             context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = mInflater.inflate(R.layout.wildlifelist, parent, false);
    ViewHolder holder = new ViewHolder();

    holder.img = (ImageView) view.findViewById(R.id.img); 
    holder.mname = (TextView) view.findViewById(R.id.maori);   
    holder.name = (TextView) view.findViewById(R.id.name);    
    holder.status = (TextView) view.findViewById(R.id.status);
    holder.check = (CheckBox) view.findViewById(R.id.check);

    view.setTag(holder);    
    return view;    
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();

    final long rowId = cursor.getLong(cursor.getColumnIndex(DBhelper.KEY_ID));

    byte[] img = cursor.getBlob(cursor.getColumnIndex(DBhelper.KEY_IMG));
    holder.img.setImageBitmap(BitmapFactory.decodeByteArray(img, 0, img.length));

    holder.mname.setText(cursor.getString(cursor.getColumnIndex(DBhelper.KEY_MNAME)));
    holder.name.setText(cursor.getString(cursor.getColumnIndex(DBhelper.KEY_NAME)));
    holder.status.setText(cursor.getString(cursor.getColumnIndex(DBhelper.KEY_STATUS)));

    holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {   
            if (isChecked) {        
                Log.w("Debug","RowID: " + rowId + ", isChecked: " + isChecked); 
                DBhelper.updateRow(rowId, 1);
            }
            else {
                Log.w("Debug","RowID: " + rowId + ", isChecked: " + isChecked);
                DBhelper.updateRow(rowId, 0);
            }   
        }
    });
}

public static class ViewHolder {
    ImageView img;
    TextView mname;
    TextView name;
    TextView status;
    CheckBox check;
} 

共有2个答案

姬英耀
2023-03-14

ListView在滚动时重用行。BindView()方法给你一行(可能是正在重用的),并允许你根据游标对象的属性设置它。看起来你并没有在BindView()中设置holder.check,这可能就是滚动ListView时它们不正确的原因。

宗翔宇
2023-03-14

使用<code>数组

CustomCursorAdapter extends CursorAdapter{

    Map<Long, Boolean> checkState = new HashMap<>();

    public CustomCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
         mInflater = (LayoutInflater) 
                 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();

         ///set other views

         holder.check.setOnCheckedChangeListener(null); //unregister the check listener

         //I like to assume all views are unchecked by default
         Boolean checked = checkState.get(rowId);
         checked = (checked == null ? false : checked);

         holder.check.setChecked(checked);

        holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {   
                //save the check state
                checkState.put(rowId, isChecked);

                if (isChecked) {        
                    Log.w("Debug","RowID: " + rowId + ", isChecked: " + isChecked); 
                    DBhelper.updateRow(rowId, 1);
                }
                else {
                    Log.w("Debug","RowID: " + rowId + ", isChecked: " + isChecked);
                    DBhelper.updateRow(rowId, 0);
                }   
            }
        });
    }
}
 类似资料:
  • 我有一个TableView,当我向下滚动表格时,复选框被取消选中。包含复选框的表格列是列,定义为:

  • 我的ListView自定义适配器(及其新实现的viewHolder)有一些问题。我有一个列表视图,每个项目都有一个复选框(这里没有新内容)。问题是,如果我的列表中有超过9个项目,当我选中第一个复选框时,第十个将自动选中(第二个与第十一个相同),就像两个项目都有一个监听器一样(我认为在某种程度上是这样)。 我在这里读到了listView、视图回收和ViewHolder解决它的方法的位置问题:如何使我

  • 我已经为recyclerView创建了一个适配器和ViewHolder。我将itemView的imageButton绑定到Viewholder中。并在onBindViewHolder中设置了onClickListener。 一切正常,但问题是,当我向下滚动列表时,imageButton的选定状态会因选定项目而改变,列表底部的一些项目已显示为选中。 下面是一些代码 适配器类 接口类 活动内的接口回调

  • 当html页面上没有任何变量可以切换时,是否有人知道如何使用selenium处理或读出复选框的状态(true/false)。我已经尝试了常用函数。isselected()和。isenabled(),但这些函数只读取不可用的值。 有人知道有没有办法得到这个身份吗?此外,xml的屏幕截图以及它是哪个元素。 如果有人有主意就太好了。我在用Java编程。

  • 我正在开发一个聊天应用程序。我有一个聊天活动,两个用户可以发送WhatsApp之类的消息,但我有个问题。 就像你在图中看到的(https://ibb.co/3cyYX01),滚动时视图乱糟糟的,我想我知道为什么了。 在查看了这些帖子后:RecyclerView在滚动时出错,Android:RecyclerView在滚动后内容出错 我假设问题可能出在函数中的回收器视图适配器中,因为我在某些视图(VI

  • 我有一个当用户滚动时显示隐藏单元格的tableView。不确定为什么会出现这种情况。 在视图中DidLoad() 用户点击按钮后,表格将以可设置动画的方式展开。 在 func tableView(_ tableView: UITableView, cellForRowAt 索引路径: 索引路径) - 当我滚动时,所有单元格都被隐藏。我看到它们是在单元格中创建的,但是,它们不会出现在我的屏幕上。为什