RxJava Observable不会发出项目

时间:2015-04-19 12:18:54

标签: android rx-java observable

我正在为kaltura视频平台编写客户端应用程序,现在,我正在开发一个根据视频列表构建的播放列表。您可以从一个列表中拖动视频,然后放到另一个列表中。

首先,我想获取所有视频(没有过滤器)并将其放入

List<KalturaVideo> sourceList;

变量。将视频添加到

List<KalturaVideo> targetList;

我想从 sourceList 中删除此项目。 所以,我有一个 KalturaVideoRetriver 类,它有一个公共方法:

public static Observable<KalturaVideo> getVideoList(Context context, String kalturaPlaylistId)

返回Observable<KalturaVideo>,我希望从此可观察项中获取KalturaVideo项。

这是 KalturaVideoRetriever 类的代码:

public class KalturaVideoRetriever {

    public static final String KALTURA_NEW_PLAYLIST_ID = "NEW_PLAYLIST";

    public static Observable<KalturaVideo> getVideoList(Context context, String kalturaPlaylistId){
        return Observable.create(new Observable.OnSubscribe<KalturaVideo>() {
            @Override
            public void call(Subscriber<? super KalturaVideo> subscriber) {
                getKalturaPlaylistContentObservable(kalturaPlaylistId, context)
                        .flatMap(new Func1<String[], Observable<?>>() {
                            @Override
                            public Observable<?> call(String[] videoIdList) {
                                return getKalturaVideoListObservable(context, videoIdList);
                            }
                        });
            }
        });
    }

    private static Observable<KalturaVideo> getKalturaVideoListObservable(Context context, String[] kalturaVideoIdArray){
        return Observable.create(new Observable.OnSubscribe<KalturaVideo>() {
            @Override
            public void call(Subscriber<? super KalturaVideo> subscriber) {
                Cursor query;
                List<KalturaVideo> result = new ArrayList<>();
                if(kalturaVideoIdArray == null || kalturaVideoIdArray.length == 0) {
                    query = context.getContentResolver().query(KalturaVideoColumns.CONTENT_URI, null, null, null, null);
                }else{
                    KalturaVideoSelection where = new KalturaVideoSelection();
                    where.kalturaIdLike(kalturaVideoIdArray);
                    query = context.getContentResolver().query(
                            KalturaVideoColumns.CONTENT_URI,
                            null,
                            where.sel(),
                            where.args(),
                            null);
                    if(!query.moveToFirst()){
                        subscriber.onCompleted();
                    }
                    KalturaVideoCursor cursor = new KalturaVideoCursor(query);
                    do{
                        KalturaVideo video = new KalturaVideo();
                        video.setId(cursor.getKalturaId());
                        video.setName(cursor.getName());
                        video.setDescription(cursor.getDescription());
                        video.setCategories(cursor.getCategories());
                        video.setCategoriesIds(cursor.getCategoriesIds());
                        video.setDownloadUrl(cursor.getDownloadUrl());
                        video.setThumbnailUrl(cursor.getThumbnailUrl());
                        video.setDataUrl(cursor.getDataUrl());
                        video.setDuration(cursor.getDuration());
                        subscriber.onNext(video);
                    }while (query.moveToNext());
                    subscriber.onCompleted();
                }
            }
        });
    }

    private static Observable<String[]> getKalturaPlaylistContentObservable(String kalturaPlaylistId, Context context){
        return Observable.create(new Observable.OnSubscribe<String[]>() {
            @Override
            public void call(Subscriber<? super String[]> subscriber) {
                KalturaPlaylistContentSelection where = new KalturaPlaylistContentSelection();
                where.playlistId(kalturaPlaylistId);
                Cursor query = context.getContentResolver().query(
                        KalturaPlaylistContentColumns.CONTENT_URI,
                        null,
                        where.sel(),
                        where.args(),
                        null
                );
                if(!query.moveToFirst()){
                    subscriber.onNext(new String[]{});
                    subscriber.onCompleted();
                }
                KalturaPlaylistContentCursor cursor = new KalturaPlaylistContentCursor(query);
                String[] result = new String[query.getCount()];
                int index = 0;
                do{
                    result[index] = cursor.getKalturaVideoId();
                    index++;
                }while (query.moveToNext());
                subscriber.onNext(result);
                subscriber.onCompleted();
            }
        });

    }

}

getKalturaPlaylistContentObservable - 通过 kalturaPlaylistId 获取一系列KalturaVideo ID。如果 kalturaPlaylistId 是KALTURA_NEW_PLAYLIST_ID,那么它会得到一个空数组,这意味着我想要获取我的SQLite数据库中的所有视频。

getKalturaVideoListObservable - 通过 getKalturaPlaylistContentObservable 中的视频ID数组获取KalturaVideo个项目。如果数组是空的,那么它会获取我所有的视频。

另外,我有一个活动: BuildPlaylistStep2Activity 。在此活动中,我想显示视频,如果 sourceList targetList 。 以下是活动代码:

public class BuildPlaylistStep2Activity extends BaseActivity implements View.OnTouchListener {

    public ViewHolder viewHolder;
    private View selected_item = null;
    Boolean touchFlag = false;
    boolean dropFlag = false;
    LayoutParams viewParams;

    List<KalturaVideo> sourceList;
    List<KalturaVideo> targetList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_build_playlist_step2);
        viewHolder = new ViewHolder();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getSourceListObservable(KalturaVideoRetriver.KALTURA_NEW_PLAYLIST_ID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(kalturaVideo -> {
                    sourceList.add(kalturaVideo);
                })
                .doOnCompleted(() -> {
                    viewHolder.sourceListView.setAdapter(new BuildPlaylistContentAdapter(
                            BuildPlaylistStep2Activity.this,
                            -1,
                            sourceList));
                });

    }

    @Override
    public boolean onTouch(View v, MotionEvent motionEvent) {
        switch (motionEvent.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                touchFlag = true;
                selected_item = v;
                viewParams = v.getLayoutParams();
                break;
            case MotionEvent.ACTION_UP:
                selected_item = null;
                touchFlag = false;
                break;
            default:
                break;
        }
        return false;
    }

    private Observable<KalturaVideo> getSourceListObservable(String kalturaPlaylistId){
        Observable<String[]> targetIdList = Observable.just(targetList)
                .map((List<KalturaVideo> kalturaVideos) -> {
                    if(targetList == null){
                        return new String[]{};
                    }
                    String[] result = new String[kalturaVideos.size()];
                    int index = 0;
                    for (KalturaVideo item : kalturaVideos) {
                        result[index] = item.getId();
                    }
                    return result;
                });
        return Observable.zip(
                targetIdList,
                KalturaVideoRetriver.getVideoList(BuildPlaylistStep2Activity.this, kalturaPlaylistId),
                (String[] idListOfTarget, KalturaVideo kalturaVideo) -> {
                    for (String item :idListOfTarget){
                        if(item.equals(kalturaVideo.getId())){
                            return null;
                        }
                    }
                    return kalturaVideo;
                })
                .filter(kalturaVideo -> {
                    return kalturaVideo != null;
                });
    }

    public class ViewHolder{
        ListView sourceListView;
        ListView targetListView;

        public ViewHolder() {
            this.sourceListView = (ListView)findViewById(R.id.source_listview);
            this.targetListView = (ListView)findViewById(R.id.target_listview);
        }
    }
}

getSourceListObservable - 只是从视频中删除,这会让我 KalturaVideoRetriever.getVideoList targetList

中包含的视频

从代码中可以看出,我尝试在 onPostCreate 方法中订阅 Observable 并在listview上显示数据,但没有任何反应,我不知道为什么...

1 个答案:

答案 0 :(得分:2)

你正在调用不是订阅的doOnCompleted和doOnNext方法,应该用于副作用 - 比如f.e.日志记录。要启动您的observable,您需要通过 subscribe 方法订阅。