Android:如何在ListView中删除已检查的行?

时间:2015-05-02 09:09:32

标签: android listview checkbox android-listview checkedtextview

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

            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>

这是具有布局inflater的自定义适配器使用的布局

<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个结果,即使我滚动了已经退出的列表并检查了其他一些&#34;未显示&#34;细胞

示例:

  • cell 0
  • 单元格1已检查
  • cell 2
  • cell 3
  • 单元格4已检查

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

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

编辑: 在onCreate()

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

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

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

1 个答案:

答案 0 :(得分:0)

创建新列表,仅添加未选中的行。然后更新适配器的集合并通知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();