SimpleCursorAdapter 弃用的构造函数

SimpleCursorAdapter Deprecated Constructor

我知道 SimpleCursorAdapter 的构造函数之一已被弃用。以下构造函数是正确使用的构造函数:

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)

有人可以向我解释一下最后一个参数是什么以及我应该在其中放置什么吗?

我有一个使用 SimpleCursorAdapter 并用数据填充自定义布局文件的方法:

    public void populateListView(){
    Cursor cursor = myDB.getAllRows();
    String[] fromFieldNames = new String[] {DBAdapter.KEY_NAME,DBAdapter.KEY_NUMBER,DBAdapter.KEY_EMAIL};
    int[] toViewIDs = new int[] {R.id.customRowContactName,R.id.customRowContactNumber,R.id.customRowRowEmail};

    SimpleCursorAdapter cursorAdapter;
    cursorAdapter = new SimpleCursorAdapter(getActivity(),R.layout.contacts_custom_row,cursor,fromFieldNames,toViewIDs,WHAT DO I PLACE HERE);

    myList = (ListView)view.findViewById(R.id.listViewFragment);
    myList.setAdapter(cursorAdapter);
}

我暂时在其中放了一个 1 只是为了消除语法错误,但不确定它如何影响我的程序。

用于确定适配器行为的标志;可以是 FLAG_AUTO_REQUERYandFLAG_REGISTER_CONTENT_OBSERVER 的任意组合。试试这个,它取自开发人员 Google 网站..

根据文档:

int: Flags used to determine the behavior of the adapter

这些标志用于确定 CursorAdapter 应如何监视内容更改(在数据库中添加或删除数据)

您似乎可以选择以下标志之一。但是,其中一个已被弃用,您应该避免使用它们。

如果不想监控您的内容更改,请发送 0。

如果你想跟踪任何变化,使用2(你可能需要搜索更多如何使用它)

值 0

根据this and this,你可以发送0,你的游标不会观察你的内容变化。

值 1

public static final int CursorAdapter.FLAG_AUTO_REQUERY Constant Value: 1 (0x00000001)

Added in API level 11 This constant was deprecated in API level 11. This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader.

If set the adapter will call requery() on the cursor whenever a content change notification is delivered. Implies FLAG_REGISTER_CONTENT_OBSERVER.

值 2

public static final int CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER Constant Value: 2 (0x00000002)

Added in API level 11 If set the adapter will register a content observer on the cursor and will call onContentChanged() when a notification comes in. Be careful when using this flag: you will need to unset the current Cursor from the adapter to avoid leaks due to its registered observers. This flag is not needed when using a CursorAdapter with a CursorLoader.