找到你要的答案

Q:Android: how to remove checked rows in ListView?

Q:安卓:如何删除在ListView检查了行吗?

When I click a button I make this listener find which items are checked in order to delete them

            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();

This is the list used in the preceding listener

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

This is the layout used by the custom adapter with layout 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>

It seems that Android load only a fixed number of cells. If the screen space can show only N cells then childCount will return this number even if there are more of N cells in the real list. So when I see if they are checked I get only N results, even if I scrolled the entired list and checked some other "not-showed" cell.

Example:

  • cell 0
  • cell 1 checked
  • cell 2
  • cell 3
  • cell 4 checked

If N=3 and the screen shows 3 cells from 0 to 2 I get a list of false, true, false from 0 to 2 and I miss false,true from 3-4.

There must be some mistake. I'd like to do a multiplice choice deletion in listviews but this behaviour is odd. Please tell me if there is also a better way.

EDIT: in onCreate()

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

the adapter has only a method where I populate the view inflated by its newView method with the values pointed by the Cursor.

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

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

            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>

It seems that Android load only a fixed number of cells. If the screen space can show only N cells then childCount will return this number even if there are more of N cells in the real list. So when I see if they are checked I get only N results, even if I scrolled the entired list and checked some other "not-showed" cell.

例子:

  • cell 0
  • cell 1 checked
  • cell 2
  • cell 3
  • cell 4 checked

如果n = 3,屏幕显示3细胞从0到2我得到一个虚假的,真实的,虚假的从0到2,我想假的,真正从3-4。

肯定弄错了。我想做一个multiplice列表视图选择删除但这行为很奇怪。请告诉我是否还有更好的方法。

EDIT: in onCreate()

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

适配器只有一个方法,我在观与价值指向的光标其新方法膨胀。

@Override
public void bindView(View view, Context context, Cursor cursor) 
answer1: 回答1:

Create a new list and only add unchecked rows. Then update the adapters's collection and notify 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();

创建一个新的列表,只添加未选中的行。然后更新适配器的收集和通知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();
android  listview  checkbox  android-listview  checkedtextview