2017-10-15 82 views
0

這是我的場景。我從Web服務獲取數據並在回收站視圖中顯示這些數據。之後,我打算將這些數據添加到本地sqlite數據庫,並顯示這些數據時,用戶打開應用程序沒有互聯網連接。我決定爲此使用IntentService,這樣我就可以在不阻塞主線程的情況下保存這些數據。如何訪問我的ArrayList中的數據<Parcelable>

我使用改造,GSON和RxJAVA組合從REST服務中獲取數據。

retrofit = new Retrofit.Builder() 
       .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .baseUrl(BASE_URL) 
       .build(); 

public void getAllEvent(){ 

     EventService eventService = retrofit.create(EventService.class); 
     Single<List<Event>> eventsAsSingle = eventService.getEvents(); 

     eventDisposableSingleObserver = eventsAsSingle 
       .subscribeOn(Schedulers.io()) 
       .observeOn(AndroidSchedulers.mainThread()) 
       .subscribeWith(new DisposableSingleObserver<List<Event>>() { 
        @Override 
        public void onSuccess(@NonNull List<Event> events) { 

         //eventDataList is type of ArrayList<Event> 
         eventDataList.addAll(events); 
         EventListAdapter listAdapter = new EventListAdapter(MainActivity.this,eventDataList); 
         recyclerView.setAdapter(listAdapter); 
         recyclerView.addItemDecoration(new RecyclerViewDividerVertical(5)); 
         recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this)); 

         //TODO: ADD data into SQLight 
         Intent intent = new Intent(MainActivity.this,EventCacheService.class); 
         intent.putParcelableArrayListExtra("event_data",eventDataList); 
         startService(intent); 

        } 

        @Override 
        public void onError(@NonNull Throwable e) { 
         Log.d(TAG, "on Error : " + e.getMessage()); 
        } 
       }); 
    } 

這是我的Event類。

import android.os.Parcel; 
import android.os.Parcelable; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Event implements Parcelable { 

    @SerializedName("id") 
    @Expose 
    private String id; 
    @SerializedName("type") 
    @Expose 
    private String type; 
    @SerializedName("actor") 
    @Expose 
    private Actor actor; 
    @SerializedName("repo") 
    @Expose 
    private Repo repo; 
    @SerializedName("payload") 
    @Expose 
    private Payload payload; 
    @SerializedName("public") 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    public Event() { 
    } 

    protected Event(Parcel in) { 
     this.id = in.readString(); 
     this.type = in.readString(); 
     this.actor = in.readParcelable(Actor.class.getClassLoader()); 
     this.repo = in.readParcelable(Repo.class.getClassLoader()); 
     this.payload = in.readParcelable(Payload.class.getClassLoader()); 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(this.id); 
     dest.writeString(this.type); 
     dest.writeParcelable(this.actor, flags); 
     dest.writeParcelable(this.repo, flags); 
     dest.writeParcelable(this.payload, flags); 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public Actor getActor() { 
     return actor; 
    } 

    public void setActor(Actor actor) { 
     this.actor = actor; 
    } 

    public Repo getRepo() { 
     return repo; 
    } 

    public void setRepo(Repo repo) { 
     this.repo = repo; 
    } 

    public Payload getPayload() { 
     return payload; 
    } 

    public void setPayload(Payload payload) { 
     this.payload = payload; 
    } 

    public static final Parcelable.Creator<Event> CREATOR = new Parcelable.Creator<Event>() { 
     @Override 
     public Event createFromParcel(Parcel source) { 
      return new Event(source); 
     } 

     @Override 
     public Event[] newArray(int size) { 
      return new Event[size]; 
     } 
    }; 

} 

我的服務類。

public class EventCacheService extends IntentService { 

    public EventCacheService() { 
     super("EventCacheService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     if (intent != null) { 
      ArrayList<Parcelable> eventData = intent.getParcelableArrayListExtra("event_data"); 
      System.out.println("yeee"); 
     } 
    } 
} 

如果我設置System.out.println()一個破發點,我可以看到,我已經採取從我的活動中的所有數據,以成功的服務。

enter image description here

但我不能找到一種方法來訪問這些Event類型的對象。正如大多數地方所建議的,我不能通過簡單的操作將這些對象轉換爲Event類型。

(ArrayList<Event>)intent.getParcelableArrayListExtra("event_data"); 

我收到一個錯誤說Error:(20, 99) error: incompatible types: ArrayList<Parcelable> cannot be converted to ArrayList<Event>

請指出我什麼都錯了。有沒有更好的方法來處理這個問題?

+0

的[可能的複製無法從ArrayList中投到ArrayList ](https://stackoverflow.com/questions/6951306/cannot-cast-from-arraylistparcelable-to-arraylistclsprite) –

回答

0

您的問題是error: incompatible types: ArrayList<Parcelable> cannot be converted to ArrayList<Event>

因此改變

ArrayList<Parcelable> eventData = intent.getParcelableArrayListExtra("event_data"); 

ArrayList<Event> eventData = intent.getParcelableArrayListExtra("event_data"); 
0

對不起,我的壞。我可以像這樣訪問這些對象。

for (Parcelable parceledEvent: eventData){ 
     Event event = (Event) parceledEvent; 
     String type = event.getType(); 
     Log.d(TAG,"type: "+type); 
} 
相關問題