getLoaderManager 已在 android 28 中弃用。ViewModel、Repository 和 Room 替换了加载器功能
我找到了2篇文章:
和
但两者都没有显示如何将光标传递给我的自定义适配器(扩展 RecyclerViewCursorAdapter)。
我的播放列表视图模型
public class PlaylistViewModel extends AndroidViewModel {
@NonNull
private MutableLiveData<Cursor> mLiveData = new MutableLiveData<>() ;
@NonNull
private final DatabaseQueryRepository mDatabaseRepository;
private LiveData<Cursor> mResult;
public PlaylistViewModel(Application application) {
super(application);
mDatabaseRepository = DatabaseQueryRepository.newInstance();
loadData();
}
private void loadData() {
mResult= mDatabaseRepository.fetchData(getApplication().
getContentResolver());
}
}
我的数据库存储库
public class DatabaseQueryRepository {
private static final int TOKEN_QUERY=0;
private QueryHandler mQueryHandler;
private String[] datacolumns;
private String sortorder;
@NonNull
public static DatabaseQueryRepository newInstance(){
return new DatabaseQueryRepository();
}
public MutableLiveData<Cursor> fetchData(ContentResolver contentResolver){
mQueryHandler = new QueryHandler(contentResolver);
datacolumns = new String[]{
MediaStore.Audio.Playlists.DATA,
MediaStore.Audio.Playlists.NAME,
MediaStore.Audio.Playlists._ID};
sortorder = MediaStore.Audio.Playlists.NAME;
Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
final String criteria =
MediaStore.Audio.Playlists.NAME
;
final MutableLiveData<Cursor> result = query(TOKEN_QUERY,uri,datacolumns,criteria, null, sortorder );
return result;
}
private MutableLiveData<Cursor> query(int token,
Uri uri,
@Nullable String[] projection,
@Nullable String selection,
@Nullable String[] selectionArgs,
@Nullable String orderby
){
final MutableLiveData<Cursor> result = new MutableLiveData<>();
mQueryHandler.startQuery(token, result, uri,projection,selection, selectionArgs, orderby );
return result;
}
private static class QueryHandler extends AsyncQueryHandler {
public QueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
// super.onQueryComplete(token, cookie, cursor);
MutableLiveData<String> mutabledata = (MutableLiveData<String>) cookie;
try {
switch (token) {
case TOKEN_QUERY:
if (cursor != null && cursor.moveToFirst()) {
String demostring = cursor.getString(0);
int test = cursor.getCount();
String playlist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.NAME));
long playlist_id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Playlists._ID));
// ((MutableLiveData<String>) cookie).setValue(demostring);
}
break;
}
} finally {
if (cursor!=null){
// cursor.close();
}
}
}
@Override
public void startQuery(int token, Object cookie, Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy) {
super.startQuery(token, cookie, uri, projection, selection, selectionArgs, orderBy);
}
}
在受保护的 void onQueryComplete 中,游标返回值 这是我想要的游标,但示例代码将其关闭。????
在我的片段中,我设置了观察者
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
PlaylistViewModel viewModel =
ViewModelProviders.of(this).get(PlaylistViewModel.class);
viewModel.getData().observe(this, data -> {
});
问题: - 我如何获得光标。我从文档中知道它应该通过 ViewModel。- 我需要将光标传递给适配器还是可以从适配器内部访问光标。- 我对创建游标的存储库感到困惑,但是 mutableLivedata 以及如何访问它呢?