アプリが初めて起動されたときにオブジェクトのリストを sharedpreference に保存しようとしています。このオブジェクトのリストをその後のアプリの使用で使用します。私のクラス:
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
public class MyClass {
private int a;
private String b;
private int c;
private ArrayList<NameValuePair> d;
public MyClass(int a, String b, int c) {
super();
this.a = a;
this.b = b;
this.c = c;
}
public MyClass(int a, String b, int c, ArrayList<NameValuePair> d) {
super();
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
public ArrayList<NameValuePair> getD() {
return d;
}
public void setD(ArrayList<NameValuePair> d) {
this.d = d;
}
public static List<MyClass> getMyObjects() {
// TODO Auto-generated method stub
List<MyClass> myObjects = new ArrayList<MyClass>();
ArrayList<NameValuePair> temp1 = new ArrayList<NameValuePair>();
temp1.add(new BasicNameValuePair("1", "20"));
temp1.add(new BasicNameValuePair("2", "30"));
temp1.add(new BasicNameValuePair("3", "40"));
ArrayList<NameValuePair> temp2 = new ArrayList<NameValuePair>();
temp2.add(new BasicNameValuePair("1", "50"));
temp2.add(new BasicNameValuePair("2", "60"));
temp2.add(new BasicNameValuePair("3", "70"));
ArrayList<NameValuePair> temp3 = new ArrayList<NameValuePair>();
temp3.add(new BasicNameValuePair("1", "50"));
temp3.add(new BasicNameValuePair("2", "60"));
temp3.add(new BasicNameValuePair("3", "70"));
myObjects.add(new MyClass(1, "ABC", 20, temp1));
myObjects.add(new MyClass(2, "DEF", 30, temp2));
myObjects.add(new MyClass(3, "GHI", 40, temp3));
return myObjects;
}
}
オブジェクトのリストを作成し、次のコード行を使用して sharedpreference に保存しました。
List<MyClass> myObjects = MyClass.getmyObjects();
String myObjectsJSONString = new Gson().toJson(myObjects);
prefsEditor.putString("MYOBJECTS", myObjectsJSONString);
myObjects を sharedpreference のサンプルに書き込んでいるときに期待どおりに形成された JSON 文字列:
[{"d":[{"name":"1","value":"20"},{"name":"2","value":"30"},{"name":"3","value":"40"}],"b":"ABC","c":20,"a":1},{"d":[{"name":"1","value":"50"},{"name":"2","value":"60"},{"name":"3","value":"70"}],"b":"DEF","c":30,"a":2},{"d":[{"name":"1","value":"50"},{"name":"2","value":"60"},{"name":"3","value":"70"}],"b":"GHI","c":40,"a":3}]
その後の使用では、次のコード行を使用してリストにデータを入力しようとしています:
String myobjectsJSONString =appPrefs.getString("MYOBJECTS", null);
Type type = new TypeToken < List <MyClass>> () {}.getType();
List <MyClass> myObjects = new Gson().fromJson(myobjectsJSONString, type);
タイプの属性「d」なしで、共有設定からオブジェクトのリストを保存および生成できますArrayList<NameValuePair>
。
しかし、属性の1つとして「d」を使用すると、オブジェクトを生成できません。
表示されるエラー: java.lang.RuntimeException: Unable to invoke no-args constructor for interface org.apache.http.NameValuePair. このタイプの InstanceCreator を Gson に登録すると、この問題が解決する場合があります
上記の例外は、sharedpreference に格納されている json からオブジェクトを設定しようとしたときに発生しました