【问题标题】:Android: how to remove checked rows in ListView?Android:如何删除 ListView 中的选中行?
【发布时间】:2015-07-12 02:07:10
【问题描述】:

当我点击一个按钮时,我会让这个监听器找到哪些项目被选中以便删除它们

            SparseBooleanArray checkedItemPositions = list.getCheckedItemPositions();
            int itemCount = list.getChildCount();
            for(int i=0; i<itemCount; i++){
                    if(checkedItemPositions.get(i))
                        Log.v("DELETE TEST - position ", Integer.toString(i)+ " item checked");
                    else
                        Log.v("DELETE TEST - position ", Integer.toString(i)+ " item not checked");
                }
                checkedItemPositions.clear();

这是前面监听器中使用的列表

  <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:choiceMode="multipleChoice">
  </ListView>

这是带有布局充气器的自定义适配器使用的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">

   <CheckedTextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="name"
       android:id="@+id/name"
       android:checkMark="?android:attr/listChoiceIndicatorMultiple"
       android:gravity="left|center_vertical" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="field1"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="right"/>
        <TextView
            android:id="@+id/number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="number"
            android:layout_weight="2"
            android:layout_gravity="center"/>
    </LinearLayout>

Android 似乎只加载固定数量的单元格。 如果屏幕空间只能显示 N 个单元格,则即使实际列表中有更多 N 个单元格,childCount 也会返回此数字。 因此,当我查看它们是否被检查时,我只得到 N 个结果,即使我滚动了整个列表并检查了其他一些“未显示”的单元格。

例子:

  • 单元格 0
  • 已选中单元格 1
  • 单元格 2
  • 细胞 3
  • 已选中单元格 4

如果 N=3 并且屏幕显示从 0 到 2 的 3 个单元格,我会得到一个从 0 到 2 的 false、true、false 列表,而我错过了 3-4 的 false、true。

一定有什么错误。我想在列表视图中进行多项选择删除,但这种行为很奇怪。请告诉我是否还有更好的方法。

编辑: 在 onCreate() 中

Cursor cur = db.query(...);
adapter = new MyCursorAdapter(this, cur, 0);
list.setAdapter(adapter);

适配器只有一个方法,我用光标指向的值填充由其 newView 方法膨胀的视图。

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

【问题讨论】:

    标签: android listview checkbox android-listview checkedtextview


    【解决方案1】:

    创建一个新列表并仅添加未选中的行。然后更新适配器的集合并通知datasetchanded

    SparseBooleanArray newList = new SparseBooleanArray();
    
    SparseBooleanArray checkedItemPositions = list.getCheckedItemPositions();
    int itemCount = list.getChildCount();
    for(int i=0; i<itemCount; i++){
        if(checkedItemPositions.get(i)){
            Log.v("DELETE TEST - position ", Integer.toString(i)+ " item checked");
        }
    
        else{
            Log.v("DELETE TEST - position ", Integer.toString(i)+ " item not checked");
            newList.add(checkedItemPositions.get(i));
        }
    }
    checkedItemPositions.clear();
    
    theListUsedInAdapter.clear();
    theListUsedInAdapter.addAll(newList);
    adapter.notifyDataSetChanged();
    

    【讨论】:

    • list 是 ListView 类型,没有 clear() 或 addAll。 newList有什么用?
    • 问题是我无法获取所有未选中的行,因为 getCheckeditemPositions 返回只检查屏幕大小中的可见行
    • 事实上我做错了,你能用你的适配器从你的活动/片段中给我一个代码吗,新列表将只包含要在列表视图上显示的内容。在我的示例中它是错误的,我会在收到您的代码后更新它
    • 我做了一个更新,我只在 onCreate 中使用适配器,并且每当我更改光标时都会更改它
    • 哦,你使用光标适配器,它比使用数组适配器更难工作......
    猜你喜欢
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多