send List of objects with service using Parcelable [duplicate]

戏子无情 提交于 2019-12-13 04:08:12

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/21100157/send-list-of-objects-with-service-using-parcelable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!