public void Savepic() {
ArrayList<DBBean> arrayList = new ArrayList();//新建一个数组 类型根据自己自定义
JSONObject jsonObj = null; //
//防止存储没用的数据,这里只存需要的参数
for (int i = 0; i < imagelist.length; i++) { //把需要存储的arraylist的数据 循环添加至一个 文件
if (null != imagelist[i] && !imagelist[i].equals("")) {
DBBean bean = new DBBean(i + 1 + "", imagelist[i]);//这里也是新建数据类型,看不懂就没必要往下看了
arrayList.add(bean);
}
}
s = new Gson().toJson(arrayList);//用gson转换成json字符串
SharedPreferencesHelper sharedPreferencesHelper = new SharedPreferencesHelper(this, SharedPreferencesHelper.CHENGSU);//本地使用String存储
sharedPreferencesHelper.put("pics", s);//参数名字和刚才转换的String字符串
String ss = (String) sharedPreferencesHelper.getSharedPreference("pics", "");//取值
KLog.e("tag", ss);
parseJSONWithJSONObject(ss);//将存的字符串转换成数据
}
获取数据
private void parseJSONWithJSONObject(String JsonData) {
try {
JSONArray jsonArray = new JSONArray(JsonData);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("pics");
String name = jsonObject.getString("s");
KLog.e("tag", "id" + id + ";name" + name + ";version");
}
} catch (Exception e) {
e.printStackTrace();
}
}