这是我的场景。我从web服务中获取数据,并在回收器视图中显示这些数据。之后,我计划将这些数据添加到本地sqlite数据库中,当用户在没有互联网连接的情况下打开应用程序时显示这些数据。我决定为此使用IntentService,这样就可以在不阻塞主线程的情况下保存这些数据。
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()
我可以看到,我已经成功地将活动中的所有数据提供给服务。
事件
事件
通过简单的操作输入。
(ArrayList<Event>)intent.getParcelableArrayListExtra("event_data");
我在说一个错误
Error:(20, 99) error: incompatible types: ArrayList<Parcelable> cannot be converted to ArrayList<Event>
请指出我做错了什么。有没有更好的方法来处理这个问题?