RxJava Observable 不发射项目
RxJava Observable doesn't emit item
我正在为 kaltura 视频平台编写一个客户端应用程序,现在,我正在开发一个从视频列表构建的播放列表。您可以将视频从一个列表拖放到另一个列表。
首先,我想获取所有视频(不带过滤器)并将其放入
List<KalturaVideo> sourceList;
变量。当您将视频添加到
List<KalturaVideo> targetList;
我想从 sourceList 中删除此项。
所以,我有一个 class KalturaVideoRetriver,它有一个 public 方法:
public static Observable<KalturaVideo> getVideoList(Context context, String kalturaPlaylistId)
它 returns Observable<KalturaVideo>
,我想从这个 observable 中得到 KalturaVideo
项。
这是 KalturaVideoRetriever 的代码 class:
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
项。如果数组为空,那么它会获取我拥有的所有视频。
此外,我还有一个 Activity:BuildPlaylistStep2Activity。在此 activity 中,如果 sourceList 和 targetList,我想显示视频。
这是activity的代码:
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,[=40= 中包含的视频]目标列表
正如您从代码中看到的,我尝试在 onPostCreate 方法中订阅 Observable 并在 listview 上显示数据,但没有任何反应,我也不知道为什么...
您调用的 doOnCompleted 和 doOnNext 方法不是订阅,应该用于副作用 - 如 f.e。记录。要启动您的可观察对象,您需要通过 subscribe 方法进行订阅。
我正在为 kaltura 视频平台编写一个客户端应用程序,现在,我正在开发一个从视频列表构建的播放列表。您可以将视频从一个列表拖放到另一个列表。
首先,我想获取所有视频(不带过滤器)并将其放入
List<KalturaVideo> sourceList;
变量。当您将视频添加到
List<KalturaVideo> targetList;
我想从 sourceList 中删除此项。 所以,我有一个 class KalturaVideoRetriver,它有一个 public 方法:
public static Observable<KalturaVideo> getVideoList(Context context, String kalturaPlaylistId)
它 returns Observable<KalturaVideo>
,我想从这个 observable 中得到 KalturaVideo
项。
这是 KalturaVideoRetriever 的代码 class:
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
项。如果数组为空,那么它会获取我拥有的所有视频。
此外,我还有一个 Activity:BuildPlaylistStep2Activity。在此 activity 中,如果 sourceList 和 targetList,我想显示视频。 这是activity的代码:
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,[=40= 中包含的视频]目标列表
正如您从代码中看到的,我尝试在 onPostCreate 方法中订阅 Observable 并在 listview 上显示数据,但没有任何反应,我也不知道为什么...
您调用的 doOnCompleted 和 doOnNext 方法不是订阅,应该用于副作用 - 如 f.e。记录。要启动您的可观察对象,您需要通过 subscribe 方法进行订阅。