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

时间:2017-10-15 13:36:45

标签: java android parcelable

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

我正在使用改造,GSON和RxJAVA组合来从REST服务中获取数据。

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

如果我在Event上设置了一个断点,我可以看到我已将我的活动中的所有数据都成功提供给服务。

enter image description here

但我无法找到访问这些Event类型对象的方法。正如在大多数地方所建议的那样,我无法通过简单地将这些对象转换为(ArrayList<Event>)intent.getParcelableArrayListExtra("event_data"); 类型。

Error:(20, 99) error: incompatible types: ArrayList<Parcelable> cannot be converted to ArrayList<Event>

我收到错误{{1}}

请指出我的错误。有没有更好的方法来解决这个问题?

2 个答案:

答案 0 :(得分:0)

抱歉,我的不好。我可以像这样访问那些对象。

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

答案 1 :(得分: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");