私はParcelableを実装するカスタムモデルクラスを持っています。それは、3つの文字列、すなわちString id_url、position、typeを含む。完全なクラスがある:カスタムArraylistをSharedPreferencesに保存してそこから取得しますか?
public class Facebook_Images_Data implements Parcelable {
String id_url;
String position;
String type;
public Facebook_Images_Data(
String id_url, String position, String type) {
this.id_url = id_url;
this.position = position;
this.type = type;
}
public String getId_url() {
return id_url;
}
public void setId_url(String id_url) {
this.id_url = id_url;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
protected Facebook_Images_Data(Parcel in) {
id_url = in.readString();
position = in.readString();
type = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id_url);
dest.writeString(position);
dest.writeString(type);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Facebook_Images_Data> CREATOR = new Parcelable.Creator<Facebook_Images_Data>() {
@Override
public Facebook_Images_Data createFromParcel(Parcel in) {
return new Facebook_Images_Data(in);
}
@Override
public Facebook_Images_Data[] newArray(int size) {
return new Facebook_Images_Data[size];
}
};
}
このモデルのArrayList
を作成した後、私はArrayListにいくつかの値を渡す必要があります。ここまで、うまく動作します。しかし、これをsharedpreferencesにどのように格納し、それを後で取り出すのですか?
List<Facebook_Images_Data> yourList = new ArrayList<Facebook_Images_Data>();
Gson gson = new Gson();
String valuesString = gson.toJson(yourList);
prefsEditor.putString("YOUR_KEY",valuesString);
そして、あなたが使用することができ、それを取り戻すために:あなたはこれを達成するためにgsonを使用することができます
前に同じ行には、おそらくこのhttp://stackoverflow.com/a/38307804の複製/ 5305430 – sushildlh