Sp保存
程序员文章站
2022-03-11 21:13:49
SharedPreferences sp = getSharedPreferences("a", MODE_PRIVATE);SharedPreferences.Editor edit = sp.edit();edit.putBoolean("isFirst", false);edit.putString("json", sb.toString());edit.commit();Result result = new Gson().fromJson(sb.toString(), Result.cl...
SharedPreferences sp = getSharedPreferences("a", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean("isFirst", false);
edit.putString("json", sb.toString());
edit.commit();
Result result = new Gson().fromJson(sb.toString(), Result.class);
Message msg = new Message();
msg.obj = result;
handler.sendMessage(msg);
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
Result res = (Result) msg.obj;
List<Result.DataBean> data = res.getData();
datas.addAll(data);
resultAdapter.notifyDataSetChanged();
return false;
}
});
SharedPreferences sp = getSharedPreferences("a", MODE_PRIVATE);
boolean isFirst = sp.getBoolean("isFirst", true);
if(isFirst){
initData();
}else{
String json = sp.getString("json", "");
Result result = new Gson().fromJson(json, Result.class);
List<Result.DataBean> data = result.getData();
datas.addAll(data);
resultAdapter.notifyDataSetChanged();
}
*仅供参考
本文地址:https://blog.csdn.net/qq_46237697/article/details/107583839