【问题标题】:send List of objects with service using Parcelable [duplicate]使用 Parcelable 发送具有服务的对象列表 [重复]
【发布时间】:2014-01-14 18:26:39
【问题描述】:

首先,我需要从 FileUtils 类中检索对象列表。 我有返回列表的 readFromAssets(Contex contex) 方法。所以我正在从 FileUtils 创建对象并且我正在获取列表。

之后我尝试实现 Parcelable 以便我可以发送列表(我不知道我的实现是否正常)。

最后发送列表...

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

但我没有得到结果...我的问题是...

  • 我的 Parcelable 实现是否正常,或者我应该这样做,以便我可以发送我的对象列表?
  • 我是否正确发送 Parselable 对象,或者我应该这样做?

提前致谢!

【问题讨论】:

    标签: android list parcelable intentservice


    【解决方案1】:

    您需要做的就是让您的Note Parcelable 如下所示:

    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];
            }
        };
    
    
    }
    

    只需将 Note 列表放入 intent extra。

    【讨论】:

    • 我很困惑!?我已经有几个参数的域类注释,Id,内容,dateCreated...等(所有的getter和setter),我需要把它们全部写在writeToParcel()方法和构造函数(Parcel in)中???。 ..... EDIT 我可以在我的域类中实现 parcelable 还是需要创建一个新的?
    • 你可以在域类中实现parceble。是的,您确实需要将它们全部写入 writeToParcel 并从 in.readString(); 的构造函数中获取它们
    猜你喜欢
    • 2018-12-15
    • 2016-09-10
    • 2013-04-10
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多