我在用Loader
for RecyclerView.Adapter
列出项目。我想列出数据库表中的特定项目。所以我做了:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selectionArgs1[]={"1","13","14"};
String selection1 = DatabaseOpenHelper.COLUMN_ID + " in (";
for (int i = 0; i < selectionArgs1.length; i++) {
selection1 += "?, ";
}
selection1 = selection1.substring(0, selection1.length() - 2) + ")";
String[] projection1 =...
return new CursorLoader(getActivity(),StudentContentProvider.CONTENT_URI1, projection1, selection1,selectionArgs1, null);
}
通常我给null,null
to selection
and selectionArgs
但在这里,我列出了具体的项目IDs
。当新项目添加到表中并且我想列出它时,就会出现问题。这cursor
我返回时不知道有新项目,因为我提供了 3 个特定项目,因此它只会检测这 3 个项目何时有变化。
当有新项目添加时如何列出ID
我也想列出它?我应该将数据库中的所有项目投影到RecyclerView.Adapter
和过滤器IDs
in onBindViewHolder()
?
既然我已经限制了IDs
in cursor
,如果仅特定项目发生变化,则它会发生变化,而不是在添加新项目时发生变化。我做了一个把戏onLoadFinished()
创造新的cursor
并交换新的光标。因此,当发生变化时,我会用我的光标获得新的光标selection
and selectionArgs
again:
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
switch (loader.getId()) {
case LOADER_ID:{
String selectionArgs1[]={...};
String selection1 = DatabaseOpenHelper.COLUMN_ID + " in (";
for (int i = 0; i < selectionArgs1.length; i++) {
selection1 += "?, ";
}
selection1 = selection1.substring(0, selection1.length() - 2) + ")";
String[] projection1 =...
mDataset1 = getActivity().getContentResolver().query(StudentContentProvider.CONTENT_URI1, projection1, selection1, selectionArgs1, null);
mAdapter.swapCursor(mDataset1);
break;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)