如何访问我的 ArrayList<Parcelable> 中的数据

How to access data inside my ArrayList<Parcelable>

这是我的场景。我从 Web 服务获取数据并在回收站视图中显示这些数据。之后,我计划将这些数据添加到本地 sqlite 数据库中,并在用户打开没有互联网连接的应用程序时显示这些数据。我决定为此使用 IntentService,这样我就可以在不阻塞主线程的情况下保存这些数据。

我正在使用改造、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 class.

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

}

我的服务class.

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() 上设置断点,我可以看到我已成功将 activity 中的所有数据提取到服务中。

但是我找不到访问那些 Event 类型对象的方法。正如大多数地方所建议的那样,我无法通过简单地将这些对象转换为 Event 类型。

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

我收到一条错误消息 Error:(20, 99) error: incompatible types: ArrayList<Parcelable> cannot be converted to ArrayList<Event>

请指出我哪里出错了。有没有更好的方法来处理这个问题?

对不起,我的错。我可以像这样访问这些对象。

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

你的问题是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");