0

更新:我每次都能在我的 Galaxy S2 上复制这个问题(有和没有调试模式),但从来没有在模拟器上!

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

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

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

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

我在使用 ListView 时遇到另一个问题(Android ListView 项目在滚动时重叠),并且我使用的是相同的 ListView,所以问题可能相关吗?数据库更新后是否有一些步骤可以强制ListView重绘?还是由于我实施解决方案的方式错误而没有自动发生?提前致谢!

这是ListView

<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;
    }
4

1 回答 1

1

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

刚看了下代码。最好使用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;
}
于 2013-02-07T11:19:34.580 回答