android数据存放map_android将hashmap保存到SharedPreferences中

The RecyclerView in activity_main has three check boxes and at the bottom of activity_main is a button which when clicked saves the checkbox status(true or false) and its adapter position to hashmap. Position is the key and status is the value.Both are converted to String.I want to save it to sharedpreferences.I found that primitive data types can be saved to shared preferences.Is there a way to save hashmap to it.

RecyclerAdapter.java:

public class RecyclerHolder extends RecyclerView.ViewHolder {

AnimCheckBox checkBox;

checkBox = (AnimCheckBox) itemView.findViewById(R.id.checkBox);

checkBox.setChecked(false);

public RecyclerHolder(final View itemView) {

super(itemView);

checkBox.setOnCheckedChangeListener(new AnimCheckBox.OnCheckedChangeListener() {

@Override

public void onChange(boolean checked) {

checkboxclick = true;

if (checkBox.isChecked()) {

boolean check = true;

clicked_position = getAdapterPosition();

String position = Integer.toString(clicked_position);

String check_status = Boolean.toString(check);

hashMap.put(position, check_status);

} else {

String removekey = String.valueOf(getAdapterPosition());

if (hashMap.containsKey(removekey)) {

hashMap.remove(removekey);

}

}

}

});

SharedPreferences sharedPreferences = context.getSharedPreferences("Main", Context.MODE_PRIVATE);

bt_click_status = sharedPreferences.getBoolean("BtnCheck", false);

if (hashMap != null && bt_click_status == true) {

if (checkboxclick == true) {

SharedPreferences sharedPreferences1 = context.getSharedPreferences("ck_itm", Context.MODE_PRIVATE);

SharedPreferences.Editor editor = sharedPreferences1.edit();

editor.put

}

}

}

}

RecyclerAdapter(Edited).java:

GsonBuilder builder = new GsonBuilder();

Gson gson = builder.enableComplexMapKeySerialization().setPrettyPrinting().create();

Type type = new TypeToken < HashMap < String, String >> () {}.getType();

String json = gson.toJson(hashMap, type);

SharedPreferences sharedPreferences1 = context.getSharedPreferences("Ctk", Context.MODE_PRIVATE);

SharedPreferences.Editor editor = sharedPreferences1.edit();

editor.putString("chk", json);

editor.commit();

MainActivity.java:

SharedPreferences sharedPreferences1 = getApplicationContext().getSharedPreferences("Ctk", Context.MODE_PRIVATE);

String get = sharedPreferences1.getString("chk", null);

places = new Gson().fromJson(get, new TypeToken < HashMap < String, String >> () {}.getType());

s = places.keySet();

Iterator i = s.iterator();

while (i.hasNext()) {

Map.Entry entry = (Map.Entry) i.next();

Log.e("key:" + entry.getKey(), "value:" + entry.getValue());

}

Error:(Error is at "s=places.entrySet() in MainActivity.java)

03 - 09 12: 51: 53.658 29260 - 29260 / com.example.jobinsabu.destination E / AndroidRuntime:

FATAL EXCEPTION: main

java.lang.NullPointerException

at com.example.jobinsabu.destination.MainActivity$1$1.onClick(MainActivity.java: 173)

at android.view.View.performClick(View.java: 4439)

at android.widget.Button.performClick(Button.java: 148)

at android.view.View$PerformClick.run(View.java: 18395)

at android.os.Handler.handleCallback(Handler.java: 725)

at android.os.Handler.dispatchMessage(Handler.java: 92)

at android.os.Looper.loop(Looper.java: 176)

at android.app.ActivityThread.main(ActivityThread.java: 5302)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java: 511)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869)

at dalvik.system.NativeStart.main(Native Method)

解决方案

You can convert it to JSON string and then save it to shared preference

Use Gson

// convert map to JSON String

GsonBuilder builder = new GsonBuilder();

Gson gson = builder.enableComplexMapKeySerialization().setPrettyPrinting().create();

Type type = new TypeToken(){}.getType();

String json = gson.toJson(hashMap, type);

And to convert it back to hashmap

String jsonString = "JSON string";

HashMap hashMap = new Gson().fromJson(jsonString, new TypeToken>(){}.getType());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值