我Loader
用于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
于selection
和selectionArgs
,但在这里,具体的我的列表项IDs
。当新项目添加到表中并且我想列出它时,就会出现问题。的cursor
,因为我给了3个具体项目我是回不知道新的项目,所以它只是检测时,有变化对那些3项。
如何在添加一些新项目时列出ID
,我也要列出?我应该从项目数据库中的所有项目RecyclerView.Adapter
和过滤IDs
的onBindViewHolder()
?
由于我已经限制IDs
了cursor
,因此仅当特定项目发生更改时才会更改,而在添加新项目时不会更改。我做了一个技巧onLoadFinished()
来创建新的cursor
并交换新的游标。因此,当有变化,我得到新的光标,我selection
和selectionArgs
再次:
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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句