当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to save List<Model> on onSaveInstaceState and retrieve it in onRestoreInstanceState

问题描述:

I have a list that I want to store it after binding it with data so that when a user rotate the device, it won't have to download the data on the server.

Here is the Model

public class Model {

String Id, Name, Work, Phone, Lp;

public String getId() {

return Id;

}

public String getLp() {

return Lp;

}

public String getName() {

return Name;

}

public String getPhone() {

return Phone;

}

public String getWork() {

return Work;

}

public void setId(String id) {

Id = id;

}

public void setLp(String lp) {

Lp = lp;

}

public void setName(String name) {

Name = name;

}

public void setPhone(String phone) {

Phone = phone;

}

public void setWork(String work) {

Work = work;

}

}

so, how to I implement the onSaveInstanceState and onRestoreInstanceState?

Any help please...

网友答案:

onSaveInstanceState() and onRestoreInstanceState() use Bundle for the data transfer. Bundle don't support too many ways to store a list of data. The only one which could apply to your needs would be putParcelableArrayList().

For this you need to implement Parcelable with your Model and in onSaveInstanceState() you wrap the generic list with an ArrayList to put the data:

outState.putParcelableArrayList(new ArrayList<Model>(models))

Retrieving the models again from a bundle is straightforward.

分享给朋友:
您可能感兴趣的文章:
随机阅读: