我有2个listview&#39。它包含文本项。 我想从左侧列表中拖动文本项并将其拖放到右侧列表中。这个我没问题。 另外,我有一个Observable,它可以激活 KalturaVideo 对象。 以下是我如何获得此可观察性的代码: 如果对象id等于来自targetList的某个id,则此函数获取已添加到右侧列表视图中的id,并从结果中排除对象:
private Observable<KalturaVideo> getSourceListObservalbe(String kalturaPlaylistId){
Observable<String[]> targetIdList = Observable.just(targetList)
.map((List<KalturaVideo> kalturaVideos) -> {
if(kalturaVideos == null || kalturaVideos.isEmpty()){
return new String[]{""};
}
String[] result = new String[kalturaVideos.size()];
int index = 0;
for (KalturaVideo item : kalturaVideos) {
result[index] = item.getId();
}
return result;
});
return Observable.combineLatest(
targetIdList,
KalturaVideoRetriver.getVideoList(BuildPlaylistStep2Activity.this, kalturaPlaylistId),
(String[] idListOfTarget, KalturaVideo kalturaVideo) -> {
for (String item :idListOfTarget){
if(item.equals(kalturaVideo.getId())){
return null;
}
}
return kalturaVideo;
})
.doOnError(throwable -> {
int i =0;
i++;
throwable.printStackTrace();
})
.filter(kalturaVideo -> {
return kalturaVideo != null;
});
}
以下是视频回溯代码:
public class KalturaVideoRetriver {
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)
.doOnNext(kalturaVideo -> {
subscriber.onNext(kalturaVideo);
})
.doOnCompleted(() -> subscriber.onCompleted());
}
})
.subscribe();
}
});
}
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()){
query.close();
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 (cursor.moveToNext());
query.close();
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) {
if(kalturaPlaylistId.equals(KALTURA_NEW_PLAYLIST_ID)){
subscriber.onNext(new String[]{});
subscriber.onCompleted();
return;
}
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[]{});
query.close();
subscriber.onCompleted();
return;
}
KalturaPlaylistContentCursor cursor = new KalturaPlaylistContentCursor(query);
String[] result = new String[query.getCount()];
int index = 0;
do{
result[index] = cursor.getKalturaVideoId();
index++;
}while (query.moveToNext());
query.close();
subscriber.onNext(result);
subscriber.onCompleted();
}
});
}
}
首先,在retriver中,我通过播放列表ID获取视频ID。如果PlaylistId为空 - 我会收到存储在我的数据库中的所有视频ID。在此之后,我通过ID获取db的所有视频,这是我在上一步获得的。
这是功能,它可以帮助我获取所有视频并显示它:
private Observable<KalturaVideo> fillLists(){
return getSourceListObservalbe(KalturaVideoRetriver.KALTURA_NEW_PLAYLIST_ID)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(throwable -> {
Log.e("TAG", throwable.getCause().toString());
})
.doOnNext(kalturaVideo -> {
sourceList.add(kalturaVideo);
})
.doOnCompleted(() -> {
viewHolder.sourceListView.setAdapter(new BuildPlaylistContentAdapter(
BuildPlaylistStep2Activity.this,
-1,
sourceList));
viewHolder.sourceListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
KalturaVideo video = (KalturaVideo) view.getTag();
ClipData data = ClipData.newPlainText("kalturaVideoId", video.getId());
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return false;
}
});
viewHolder.targetListView.setAdapter(new BuildPlaylistContentAdapter(
BuildPlaylistStep2Activity.this,
-1,
targetList
));
});
}
以下是我对它的描述:
subscription = fillLists().subscribe();
以下是我在拖放后尝试更新2个列表的方法:
targetList.add(video);
sourceList.clear();
fillLists().subscribe();
其中视频 - 与视图相关的视频,已被删除到新列表。
所以,当我第一次将视图放到另一个列表时,它工作正常。但是当我第二次丢弃时,我在 doOnError 方法中得到了这个堆栈跟踪:
04-20 18:46:02.072 8015-8083 / com.mediamanagment.app W / System.err: java.lang.NullPointerException 04-20 18:46:02.072
8015-8083 / com.mediamanagment.app W / System.err:at com.mediamanagment.app.activity.BuildPlaylistStep2Activity.lambda $ getSourceListObservalbe $ 3(BuildPlaylistStep2Activity.java:106) 04-20 18:46:02.072 8015-8083 / com.mediamanagment.app W / System.err: 在 com.mediamanagment.app.activity.BuildPlaylistStep2Activity.access $拉姆达$ 1(BuildPlaylistStep2Activity.java) 04-20 18:46:02.072 8015-8083 / com.mediamanagment.app W / System.err: 在 com.mediamanagment.app.activity.BuildPlaylistStep2Activity $$ LAMBDA $ 2.call(未知 来源)04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err:at rx.functions.Functions $ 3.call(Functions.java:76) 04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err: 在 rx.operators.OperatorCombineLatest $ Collector.next(OperatorCombineLatest.java:157) 04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err: 在 rx.operators.OperatorCombineLatest $ SourceSubscriber.onNext(OperatorCombineLatest.java:218) 04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err: 在 com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 1 $ 1.lambda $调用$ 13(KalturaVideoRetriver.java:36) 04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err: 在 com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 1 $ $ 1.访问拉姆达$ 0(KalturaVideoRetriver.java) 04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err: 在 com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 1 $ 1 $$ LAMBDA $ 1.call(未知 来源)04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable $ 12.onNext(Observable.java:3682)04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err:at rx.operators.OperatorDoOnEach $ 1.onNext(OperatorDoOnEach.java:61)04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err:at com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 2.call(KalturaVideoRetriver.java:80) 04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err: 在 com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 2.call(KalturaVideoRetriver.java:47) 04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err: 在rx.Observable $ 2.call(Observable.java:153)04-20 18:46:02.080
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable $ 2.call(Observable.java:149)04-20 18:46:02.080
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable $ 2.call(Observable.java:153)04-20 18:46:02.080
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable $ 2.call(Observable.java:149)04-20 18:46:02.080
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable.unsafeSubscribe(Observable.java:6110)04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err:at rx.operators.OperatorMerge $ 1.onNext(OperatorMerge.java:66)04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err:at rx.operators.OperatorMerge $ 1.onNext(OperatorMerge.java:43)04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err:at rx.operators.OperatorMap $ 1.onNext(OperatorMap.java:54)04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err:at com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 3.call(KalturaVideoRetriver.java:93) 04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err: 在 com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 3.call(KalturaVideoRetriver.java:89) 04-20 18:46:02.080 8015-8083 / com.mediamanagment.app W / System.err: 在rx.Observable $ 2.call(Observable.java:153)04-20 18:46:02.080
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable $ 2.call(Observable.java:149)04-20 18:46:02.080
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable $ 2.call(Observable.java:153)04-20 18:46:02.088
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable $ 2.call(Observable.java:149)04-20 18:46:02.088
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable.subscribe(Observable.java:6177)04-20 18:46:02.088
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable.subscribe(Observable.java:5842)04-20 18:46:02.088
8015-8083 / com.mediamanagment.app W / System.err:at com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 1.call(KalturaVideoRetriver.java:41) 04-20 18:46:02.088 8015-8083 / com.mediamanagment.app W / System.err: 在 com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 1.call(KalturaVideoRetriver.java:27) 04-20 18:46:02.088 8015-8083 / com.mediamanagment.app W / System.err: 在rx.Observable.unsafeSubscribe(Observable.java:6110)04-20 18:46:02.095 8015-8083 / com.mediamanagment.app W / System.err:at rx.operators.OperatorCombineLatest.call(OperatorCombineLatest.java:87) 04-20 18:46:02.095 8015-8083 / com.mediamanagment.app W / System.err: 在 rx.operators.OperatorCombineLatest.call(OperatorCombineLatest.java:40) 04-20 18:46:02.095 8015-8083 / com.mediamanagment.app W / System.err: 在rx.Observable $ 2.call(Observable.java:153)04-20 18:46:02.095
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable $ 2.call(Observable.java:149)04-20 18:46:02.095
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable $ 2.call(Observable.java:153)04-20 18:46:02.095
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable $ 2.call(Observable.java:149)04-20 18:46:02.095
8015-8083 / com.mediamanagment.app W / System.err:at rx.Observable.unsafeSubscribe(Observable.java:6110)04-20 18:46:02.095 8015-8083 / com.mediamanagment.app W / System.err:at rx.operators.OperatorSubscribeOn $ 1 $ 1.call(OperatorSubscribeOn.java:60) 04-20 18:46:02.095 8015-8083 / com.mediamanagment.app W / System.err: 在 rx.schedulers.NewThreadScheduler $ NewThreadWorker $ ScheduledAction.run(NewThreadScheduler.java:141) 04-20 18:46:02.095 8015-8083 / com.mediamanagment.app W / System.err: 在 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:390) 04-20 18:46:02.095 8015-8083 / com.mediamanagment.app W / System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)04-20 18:46:02.095 8015-8083 / com.mediamanagment.app W / System.err:at java.util.concurrent.ScheduledThreadPoolExecutor中的$ ScheduledFutureTask.access $ 201(ScheduledThreadPoolExecutor.java:153) 04-20 18:46:02.103 8015-8083 / com.mediamanagment.app W / System.err: 在 java.util.concurrent.ScheduledThreadPoolExecutor中的$ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267) 04-20 18:46:02.103 8015-8083 / com.mediamanagment.app W / System.err: 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 04-20 18:46:02.103 8015-8083 / com.mediamanagment.app W / System.err: 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:573) 04-20 18:46:02.103 8015-8083 / com.mediamanagment.app W / System.err: 在java.lang.Thread.run(Thread.java:856)
并没有返回任何视频
答案 0 :(得分:2)
你在这段代码中得到了NullPointerException
Log.e("TAG", throwable.getCause().toString());
我认为你的异常没有原因,所以toString
调用抛出一个NPE(NullPointerException)
用
替换您的代码Log.e("TAG", throwable.getCause());
它应该解决这个问题
但是您的代码非常复杂,某些部分可以以不同的方式写入。其他一些马车。
例如:
<强>错误强>
.doOnError(throwable -> {
int i =0;
i++;
throwable.printStackTrace();
})
i
将始终等于0(或i++
之后的1)。这是自愿的吗?
错误代码
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)
.doOnNext(kalturaVideo -> {
subscriber.onNext(kalturaVideo);
})
.doOnCompleted(() -> subscriber.onCompleted());
}
})
.subscribe();
}
});
}
不要在Observable中使用Observable 。只需转换第一个
public static Observable<KalturaVideo> getVideoList(Context context, String kalturaPlaylistId){
return getKalturaPlaylistContentObservable(kalturaPlaylistId, context).flatMap((String[] videoIdList) -> getKalturaVideoListObservable(context, videoIdList));
}
复杂代码
Observable<String[]> targetIdList = Observable.just(targetList)
.map((List<KalturaVideo> kalturaVideos) -> {
if(kalturaVideos == null || kalturaVideos.isEmpty()){
return new String[]{""};
}
String[] result = new String[kalturaVideos.size()];
int index = 0;
for (KalturaVideo item : kalturaVideos) {
result[index] = item.getId();
}
return result;
});
您尝试重写Rx本身已经可以执行的代码。
Observable<String[]> targetIdList = Observable.just(targetList)
.flatMapIterable(videos -> videos)
.map(video -> video.getId())
.toList()
.map(l -> l.toArray(new String[]));