2017-10-15 162 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); 
} 
相关问题