删除所有项目后,Android ListView仍未清除

时间:2013-02-07 11:11:26

标签: android android-listview

更新:我每次都可以在我的Galaxy S2上复制此问题(有和没有调试模式),但从不在模拟器上!

我正在使用ListView上的上下文菜单(使用CursorAdapter的自定义实现)让用户选择“全部删除”选项。选择此选项后,列表中显示的所有项目都应从数据库中永久删除,然后调用适配器上的changeCursor(..)以强制列表更新。

然而,正在发生的事情是,即使从数据库中删除记录并调用changeCursor(..),这些项目也是可见的。只有项目分隔符消失。只有在我触摸列表中的某个位置后,才能清除这些项目。

当用户激活上下文菜单时: http://i.stack.imgur.com/ivFvJ.png

从数据库中删除并调用changeCursor(..)后: http://i.stack.imgur.com/CX6BM.png

我遇到了ListView(Android ListView items overlap while scrolling)的另一个问题,我使用相同的ListView,所以问题可能是相关的吗?在数据库更新后是否有一些强制ListView重绘的步骤?或者由于我实施解决方案的错误而不会自动发生?提前谢谢!

以下是ListView

的XML
<ListView
        android:id="@+id/all_reminders_list"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_alignParentLeft="true"
        android:clickable="true"
        android:dividerHeight="1.0sp"
        android:animateLayoutChanges="true">

以下是我的自定义newView(..)

CursorAdapter方法
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.view_list_item, parent, false);
    return view;
}

bindView(..)

CursorAdapter方法
public void bindView(View view, Context context, Cursor cursor) {

        TextView whatTextView = (TextView) view.findViewById(R.id.item_what_text);
        whatTextView.setText(cursor.getString(1));
        TextView whenTextView = (TextView) view.findViewById(R.id.item_when_text);


        if(cursor.getInt(9) != 0) // DONE_FLAG = 1 (completed)
        {
            //Arrow visibility
            ImageView arrow = (ImageView)view.findViewById(R.id.list_item_arrow);
            arrow.setVisibility(View.INVISIBLE);

            //Text color
            whatTextView.setTextColor(Color.LTGRAY);
            whenTextView.setTextColor(Color.LTGRAY);

            //WHEN text
            whenTextView.setText(TimeCalculationHelper.getCompletedTimeString(cursor.getLong(2)));
        }
        else // DONE_FLAG = 0
        {
            //Arrow visibility
            ImageView arrow = (ImageView)view.findViewById(R.id.list_item_arrow);
            arrow.setVisibility(View.VISIBLE);

            //Text color
            whatTextView.setTextColor(Color.BLACK);
            whenTextView.setTextColor(Color.BLACK);

            //WHEN text
            whenTextView.setText(TimeCalculationHelper.getTimeRemainingString(cursor.getLong(2)));


        }
}

以下是onContextItemSelected(..)中包含Activity

ListView方法
public boolean onContextItemSelected(MenuItem item)
    {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        ListView allRemindersList = (ListView)findViewById(R.id.all_reminders_list);

        switch (item.getItemId()) {
        case R.id.delete_item:
            //Delete the selected reminder from the database
            databaseHelper.deleteRowByID(info.id);

            //Refresh the main activity list
            ((ActiveRemindersAdapter) allRemindersList.getAdapter()).changeCursor(databaseHelper.getAllRemindersForList());
            return true;

        case R.id.delete_done:
            //Delete all reminders with DONE_FLAG = 1
            databaseHelper.deleteDoneReminders();

            //Refresh the main activity list
            ((ActiveRemindersAdapter) allRemindersList.getAdapter()).changeCursor(databaseHelper.getAllRemindersForList());
        }
        return false;
    }

1 个答案:

答案 0 :(得分:1)

在光标更改后调用适配器上的 notifyDataSetChanged()以重新加载视图。 如果运行预蜂窝设备,最好使用CursorAdapter中的SupportLibrary

只是查看代码。更好地使用 swapCursor(),它会自动注册新的内容观察者并为您调用 notifyDataSetChanged()

来自CursorAdapter源代码。

/**
 * Change the underlying cursor to a new cursor. If there is an existing cursor it will be
 * closed.
 * 
 * @param cursor The new cursor to be used
 */
public void changeCursor(Cursor cursor) {
    Cursor old = swapCursor(cursor);
    if (old != null) {
        old.close();
    }
}

/**
 * Swap in a new Cursor, returning the old Cursor.  Unlike
 * {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
 * closed.
 *
 * @param newCursor The new cursor to be used.
 * @return Returns the previously set Cursor, or null if there wasa not one.
 * If the given new Cursor is the same instance is the previously set
 * Cursor, null is also returned.
 */
public Cursor swapCursor(Cursor newCursor) {
    if (newCursor == mCursor) {
        return null;
    }
    Cursor oldCursor = mCursor;
    if (oldCursor != null) {
        if (mChangeObserver != null) oldCursor.unregisterContentObserver(mChangeObserver);
        if (mDataSetObserver != null) oldCursor.unregisterDataSetObserver(mDataSetObserver);
    }
    mCursor = newCursor;
    if (newCursor != null) {
        if (mChangeObserver != null) newCursor.registerContentObserver(mChangeObserver);
        if (mDataSetObserver != null) newCursor.registerDataSetObserver(mDataSetObserver);
        mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
        mDataValid = true;
        // notify the observers about the new cursor
        notifyDataSetChanged();
    } else {
        mRowIDColumn = -1;
        mDataValid = false;
        // notify the observers about the lack of a data set
        notifyDataSetInvalidated();
    }
    return oldCursor;
}