0

This is my scenario. I get data from a web service and display those data in recycler view. After that I'm planing to add those data in to local sqlite database and display those data when user open application without internet connection. I decided to use IntentService for this so I can save those data without blocking main thread.

I'm using retrofit, GSON and RxJAVA combination to take data from REST service.

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

Here's my 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];
        }
    };

}

My service 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");
        }
    }
}

If I set a break point on System.out.println() I can see that I have taken all the data from my activity to Service successfully.

enter image description here

But I can't find a way to access those Event type objects. As suggested in most places I can't convert these objects in to Event type by simply doing.

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

I'm getting an error saying Error:(20, 99) error: incompatible types: ArrayList<Parcelable> cannot be converted to ArrayList<Event>

Please point out what I'm going wrong. Is there any better way to handle this ?

  • Possible duplicate of [Cannot cast from ArrayList to ArrayList](https://stackoverflow.com/questions/6951306/cannot-cast-from-arraylistparcelable-to-arraylistclsprite) – Mohamed_AbdAllah Oct 15 '17 at 13:50

2 Answers2

0

Sorry, My bad. I can access those objects like this.

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

You problem is error: incompatible types: ArrayList<Parcelable> cannot be converted to ArrayList<Event>

So change

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

to

ArrayList<Event> eventData = intent.getParcelableArrayListExtra("event_data");
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42