Question

First of all i need to retrieve the list of objects from FileUtils class. There i have the readFromAssets(Contex contex) method which returns the list. So i'm creating object from FileUtils and i'm getting the list.

After that i have tried to implement Parcelable so i can send the list(i dont know if my implementation is OK).

And at the end to send the the list...

public class NoteReaderService extends IntentService{

FileUtils utils = new FileUtils();
List<Note> noteList;
public NoteReaderService(String name) {
    super(name);
}
@Override
public void onHandleIntent(Intent intent) {

noteList = utils.readFromAssets(getBaseContext());
MyParcelable obj = new MyParcelable();
obj.setList(noteList);

Intent intentService = new Intent(Constants.BROADCAST_ACTION_INTENT);
intentService.putExtra("key", obj);
sendBroadcast(intentService);
}
public class MyParcelable implements Parcelable { 
    private List<Note> noteList;
    public int describeContents() 
    { return 0; } 
    public void writeToParcel(Parcel out, int flags) {
            out.writeList(noteList);
                    } 
    public final Parcelable.Creator<MyParcelable> CREATOR = new 
            Parcelable.Creator<MyParcelable>() {

            public MyParcelable createFromParcel(Parcel in) { 
                return new MyParcelable(in); 
            } 
            public MyParcelable[] newArray(int size) { 
                return new MyParcelable[size]; 
        }
    };  
    private MyParcelable(Parcel in) {
                in.readList(noteList, null);
    }
    public void setList(List<Note> noteList){
        this.noteList = noteList;
    }
 }

But i'm not getting a result from this... My questions are...

  • Is my implementation of the Parcelable ok, or i should do it otherwise so i can send my List of objects?
  • Am i sending the Parselable object correctly, or i should do it otherwise?

Thanks in advance!

Was it helpful?

Solution

All you need to do is make your Note Parcelable like following:

public class Note implements Parcelable {
    /**
     * 
     */
    private String name;


    public Note(Parcel in) {
        super();
        this.name = in.readString();

    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(this.name);

    }

    public static final Parcelable.Creator CREATOR = new Creator<Note>() {

        @Override
        public Note createFromParcel(Parcel source) {
            return new Note(source);
        }

        @Override
        public Note[] newArray(int size) {

            return new Note[size];
        }
    };


}

And just put the list of Note in intent extra.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top