【问题标题】:ListView non-selected items getting selectedListView 非选定项目被选中
【发布时间】:2013-06-05 22:33:23
【问题描述】:

我在由 db (SimpleCursorAdapter) 填充的应用程序中的列表视图上使用多项选择。列表视图选择有一些奇怪的行为。

如果数据库中有超过 7 个项目,如果我在列表视图中选择第一个项目,即使我没有选择第 8 个项目,第 8 个项目也会被选中,反之亦然。如果我选择第 9 项,则选择第 2 行。

这里发生了什么?

代码:

  String[] projection = { ..table_columns..};

String[] from = { table_columns..};
Cursor cursor = contentResolver.query(SomeContentProvider.CONTENT_URI, projection, null, null,
        null);

// the XML defined views which the data will be bound to 
int[] to = new int[] { 
 R.id.color,
 R.id.name,
 R.id.desc,
};


// create the adapter using the cursor pointing to the desired data 
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
 this, R.layout.layout_main, 
 cursor, 
 from, 
 to,
 0);

dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
   public boolean setViewValue(View view, Cursor cursor, int column) {
       int nNameIndex = cursor.getColumnIndexOrThrow(EventsTable.COLUMN_NAME);
       if( column == nNameIndex ){ 
           TextView nname = (TextView) view;
           String name = cursor.getString(cursor.getColumnIndex(EventsTable.COLUMN_NAME));

           String formatted_name = "NAME: " +name;

           nname.setText(formatted_name);
           return true;
       }
       return false;
   }
});

    listView.setAdapter(dataAdapter);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setOnItemLongClickListener(new OnItemLongClickListener() {
       @Override
       public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {

           if (!listView.isItemChecked(pos)){
               listView.setItemChecked(pos, true);
               v.setBackground(getResources().getDrawable(R.drawable.listview_bg_selected));
               v.setSelected(true);

           } else {
               listView.setItemChecked(pos, false); 
               v.setBackground(getResources().getDrawable(R.drawable.listview_bg));
               v.setSelected(false);
           }

           if (listView.getCheckedItemCount() > 0) {

               if (mMode == null) {
                   mMode = startActionMode(new ActionModeCallback());
               } else {
                   mMode.setTitle(listView.getCheckedItemCount() + " " + "Selected");

               }
           } else {
               if (mMode != null) {
                   mMode.finish();


               }
           }

           return true;
       }
    });

【问题讨论】:

    标签: android listview


    【解决方案1】:

    我怀疑这是因为在适配器的 bindView 中,您没有检查项目是否被选中,然后适当地更改背景。

    你正在经历你的观点被回收。

    因此,当您滚动并说项目 1 消失并被选中时,项目 1 的视图将被重新用于项目 8。

    所以在你的视图活页夹中添加这样的东西

           int post = cursor.getPosition();
           if (!listView.isItemChecked(pos)){
                   v.setBackground(getResources().getDrawable(R.drawable.listview_bg_selected));
    
           } else {
                   v.setBackground(getResources().getDrawable(R.drawable.listview_bg));
           }
    

    【讨论】:

    • 如果项目被检查,我如何检查bindView?请检查我的代码,我已经更新了。我没有使用自定义SimpleCursorAdapter
    • 添加了代码示例。该代码类似于您首先设置背景的方式。检查状态,并根据数据设置或取消设置。取消设置非常重要,因为回收视图可能已将其视图设置为反映选择。
    • 谢谢,这就像一个魅力。但奇怪的是,我失去了造型。
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多