2015-04-20 108 views
1

我有2個listview的。它包含文本項目。 我想從左側列表中拖動文本項並將其拖放到右側列表中。 Whith這我沒有問題。 另外,我有一個Observable,它暗含KalturaVideo對象。 下面是代碼,我如何得到這個觀察到: 在右側列表視圖已經加入該功能獲取的ID,並且不包括從結果對象,如果對象ID等於從TARGETLIST一些ID:RxJava從Observable更新數據

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; 
       }); 
    } 

下面是一個代碼視頻retriver:

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 
        )); 
       }); 
    } 

這裏是我如何sibscribe它:

subscription = fillLists().subscribe(); 

這裏是,如何我試圖更新2列表拖放後:

targetList.add(video); 
sourceList.clear(); 
fillLists().subscribe(); 

其中視頻 - 是與視圖相關的視頻,它被刪除到新的lis噸。

因此,當我第一次將視圖放入另一個列表時,它工作正常。但是,當我把第二次,我在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: at com.mediamanagment.app.activity.BuildPlaylistStep2Activity.access $ lambda $ 1(BuildPlaylistStep2Ac tivity.java) 04-20 18:46:02.072 8015-8083/com.mediamanagment.app W/System.err: at com.mediamanagment.app.activity.BuildPlaylistStep2Activity $$ Lambda $ 2.call(Unknown Source )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: at rx.operators.OperatorCombineLatest $ Collector.next(OperatorCombineLatest.java:157) 04-20 18:46:02.080 8015-8083/com.mediamanagment .app W/System.err: at rx.operators.OperatorCombineLatest $ SourceSubscriber.onNext(OperatorCombineLatest.java:218) 04-20 1 8:46:02.080 8015-8083/com.mediamanagment.app W/System.err: at com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 1 $ 1.lambda $ call $ 13(KalturaVideoRetriver.java:36) 04-20 18:46:02.080 8015-8083/com.mediamanagment.app W/System.err: at com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 1 $ 1。訪問$ lambda $ 0(KalturaVideoRetriver.java) 04-20 18:46:02.080 8015-8083/com.mediamanagment.app W/System.err: at com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 1 $ 1 $$ Lambda $ 1.call(Unknown Source)04-20 18:46:02.080 8015-8083/com.mediamanagment.app W/System.err:在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.e rr: at com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 2.call(KalturaVideoRetriver.java:47) 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: 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。 Operator.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: at com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 3.call(KalturaVideoRetriver.java:89) 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 $ 2.call(Observable.java:153)04-20 18:46:02.088
8015-8083/com.mediamanagment.app W/System.err:at rx.Observa ble $ 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: at com.mediamanagment.app.retrivers.KalturaVideoRetriver $ 1.call(KalturaVideoRetriver.java:27) 04- 20 18:46:02.088 8015-8083/com.mediamanagment。應用程序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 (OperatorCombineLatest.java:87) 04-20 18:46:02.095 8015-8083/com.mediamanagment.app W/40) 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 $ 2.call(Observable.java:153)04-20 18:46:02.095
8015-8083/com.mediamanagment.app W/System.err: 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: at rx.schedulers.NewThreadScheduler $ NewThreadWorker $ ScheduledAction。運行(NewThreadScheduler.java:141) 04-20 18:46:02.095 8015-8083/com.mediamanagment.app W/System.err: at 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: at java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267) 04-20 18:46:02.103 8015-8083/com.mediamanagment.app W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 04-20 18:46 :02.103 8015-8083/com.mediamanagment.app W/System.err: at 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)

並沒有什麼視頻返回

回答

2

你已經摹加時賽在此代碼

Log.e("TAG", throwable.getCause().toString()); 

我認爲你的異常沒有原因的NullPointerException異常,所以toString調用拋出NPE(空指針異常)

Log.e("TAG", throwable.getCause()); 

替換代碼它應該解決這個問題

但是你的代碼非常複雜,有些部分可以被寫en以不同的方式。其他一些越野車。

例如:

錯誤

.doOnError(throwable -> { 
    int i =0; 
    i++; 
    throwable.printStackTrace(); 
}) 

i將總是等於0(或1 i++之後)。這是自願的嗎?

錯誤代碼

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(); 
     } 
    }); 
} 

不要使用一個可觀察的可觀察。剛剛改造的第一個

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[])); 
+1

非常感謝您幫助解決我的問題並查看我的代碼。你幫了我很多。 – Alexander

相關問題