Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

rx java - Use RXAndroid with Cursor to emit data

I have following Model

public class VideosOwn {
    public long _id;
    public String _title;
    public String _width;
    public long _height;
    public String _orientation;
    public long _size;

    public VideosOwn(long _id, String _title, String _width, long _height, String _orientation, long _size) {
        this._id = _id;
        this._title = _title;
        this._width = _width;
        this._height = _height;
        this._orientation = _orientation;
        this._size = _size;

    }
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

and following method to get the data as an array using ContentResolover

public ArrayList<VideosOwn> GetVideoList(Context mContext) {
        ArrayList<VideosOwn> videos = new ArrayList<>();
        Cursor cursor = mContext.getContentResolver().query(VideosUri,
                VideosProjection, null, null, MediaStore.Video.Media.DATE_ADDED + " DESC");
        if (cursor != null && cursor.moveToFirst()) {
            do {
                VideosOwn videoval = new VideosOwn(
                        cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID)),
                        cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE)),
                        cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.WIDTH)),
                        cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.HEIGHT)),
                        cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.ORIENTATION)),
                        cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE))
                );
                videos.add(videoval);
            } while (cursor.moveToNext());
        }
        if (cursor != null) {
            cursor.close();
        }
        return videos;
    }

I am new to Rxjava/RxAndroid, I want emit The data as an ArrayList

How can that be done , Should we use Disposalbe, Observable.fromCallable, what would be the Best solution.

question from:https://stackoverflow.com/questions/65879653/use-rxandroid-with-cursor-to-emit-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As you always want a single result and it will be a cold observable, you can use a Single, something like these examples :

Emitter :

public Single<List<VideosOwn>> getVideos(Context context) {
    return Single.create(emitter -> {
        try {
            final List<VideosOwn> result = GetVideoList(context);
            emitter.onSuccess(result);
        } catch (Exception e) {
            emitter.onError(e);
        }
    });
}

Callable :

public Single<List<VideosOwn>> getVideos(Context context) {
        return Single.fromCallable(() -> GetVideoList(context));
    }

Usage :

    // Retain Disposable and call d.dispose() if you no longer want to subscribe to the result
    final Disposable d = getVideos(context)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(r -> {
                // do something with list
            }, e -> {
                // log error or some other handle
            });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...