hashmap 中的数据在关闭应用程序后不保存
Data in hashmap does not save after closing app
我目前正在 android studio 中创建一个应用程序,我将用户输入存储到哈希图中并将其显示到列表视图中,但是当我关闭应用程序并重新打开它时,数据消失了它显示一个空列表视图。
有办法解决吗?
假设你有一个 hashMap val mp = HashMap<Int, String>()
。然后你需要在你的应用程序关闭时保存这个 hashmap 的内容,并在应用程序打开时恢复数据。您可以将地图存储在 sharedPreference
或 preferenceDataStore
中。我将向您展示 sharedPreference 方式:
private val mp: HashMap<Int, String> = HashMap()
private val preferenceName = "HASHMAP_PREFS"
private val HASHMAP_KEY = "HASHMAP_KEY"
private lateinit var sharedPreferences: SharedPreferences
private fun initPref() {
if(!this::sharedPreferences.isInitialized) {
this.sharedPreferences = getSharedPreferences(preferenceName, Context.MODE_PRIVATE)
}
}
private fun saveHashMap() {
initPref()
val editor = this.sharedPreferences.edit()
val hashMapValue = Gson().toJson(mp)
editor.putString(HASHMAP_KEY, hashMapValue)
editor.apply()
}
private fun restoreHashMap() {
initPref()
val hashMapAsStringValue = this.sharedPreferences.getString(HASHMAP_KEY, null)
if(hashMapAsStringValue!=null) {
val hashMapType: Type = object : TypeToken<HashMap<Int, String>>() {}.type
val tempHashMap: HashMap<Int, String> = Gson().fromJson(hashMapAsStringValue, hashMapType)
this.mp.putAll(tempHashMap) // the hashMap is restored
}
Log.e("TAG", "hashMap = ${mp.toString()}")
}
这里,函数名是自解释的。应用关闭时简单地保存数据:
override fun onDestroy() {
saveHashMap()
super.onDestroy()
}
并在应用打开时恢复数据:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
restoreHashMap()
val key = System.currentTimeMillis().toInt() % 100
val value = "${key*2}"
mp.put(key, value)
// todo: logic, other codes...
}
注意在这个例子中,我使用了 HashMap。如果您使用其他类型的地图,您应该相应地更改代码。
编辑
这是 java 代码可以如下所示:
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.HashMap;
public class DemoActivity extends AppCompatActivity {
private HashMap<Integer, String> mp = new HashMap<>();
private String preferenceName = "HASHMAP_PREFS";
private String HASHMAP_KEY = "HASHMAP_KEY";
private SharedPreferences sharedPreferences = null;
private void initPreference() {
if(sharedPreferences == null) {
sharedPreferences = getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
}
}
private void saveHashMap() {
initPreference();
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String hashMapValue = gson.toJson(mp);
editor.putString(HASHMAP_KEY, hashMapValue);
editor.apply();
}
private void restoreHashMap() {
initPreference();
String hashMapValue = sharedPreferences.getString(HASHMAP_KEY, null);
if(hashMapValue!=null) {
Type hashMapType = new TypeToken<HashMap<Integer, String>>(){}.getType();
Gson gson = new Gson();
HashMap<Integer, String> restoredHashMap = gson.fromJson(hashMapValue, hashMapType);
mp.putAll(restoredHashMap);
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
restoreHashMap();
}
@Override
protected void onDestroy() {
saveHashMap();
super.onDestroy();
}
}
我目前正在 android studio 中创建一个应用程序,我将用户输入存储到哈希图中并将其显示到列表视图中,但是当我关闭应用程序并重新打开它时,数据消失了它显示一个空列表视图。
有办法解决吗?
假设你有一个 hashMap val mp = HashMap<Int, String>()
。然后你需要在你的应用程序关闭时保存这个 hashmap 的内容,并在应用程序打开时恢复数据。您可以将地图存储在 sharedPreference
或 preferenceDataStore
中。我将向您展示 sharedPreference 方式:
private val mp: HashMap<Int, String> = HashMap()
private val preferenceName = "HASHMAP_PREFS"
private val HASHMAP_KEY = "HASHMAP_KEY"
private lateinit var sharedPreferences: SharedPreferences
private fun initPref() {
if(!this::sharedPreferences.isInitialized) {
this.sharedPreferences = getSharedPreferences(preferenceName, Context.MODE_PRIVATE)
}
}
private fun saveHashMap() {
initPref()
val editor = this.sharedPreferences.edit()
val hashMapValue = Gson().toJson(mp)
editor.putString(HASHMAP_KEY, hashMapValue)
editor.apply()
}
private fun restoreHashMap() {
initPref()
val hashMapAsStringValue = this.sharedPreferences.getString(HASHMAP_KEY, null)
if(hashMapAsStringValue!=null) {
val hashMapType: Type = object : TypeToken<HashMap<Int, String>>() {}.type
val tempHashMap: HashMap<Int, String> = Gson().fromJson(hashMapAsStringValue, hashMapType)
this.mp.putAll(tempHashMap) // the hashMap is restored
}
Log.e("TAG", "hashMap = ${mp.toString()}")
}
这里,函数名是自解释的。应用关闭时简单地保存数据:
override fun onDestroy() {
saveHashMap()
super.onDestroy()
}
并在应用打开时恢复数据:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
restoreHashMap()
val key = System.currentTimeMillis().toInt() % 100
val value = "${key*2}"
mp.put(key, value)
// todo: logic, other codes...
}
注意在这个例子中,我使用了 HashMap
编辑
这是 java 代码可以如下所示:
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.HashMap;
public class DemoActivity extends AppCompatActivity {
private HashMap<Integer, String> mp = new HashMap<>();
private String preferenceName = "HASHMAP_PREFS";
private String HASHMAP_KEY = "HASHMAP_KEY";
private SharedPreferences sharedPreferences = null;
private void initPreference() {
if(sharedPreferences == null) {
sharedPreferences = getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
}
}
private void saveHashMap() {
initPreference();
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String hashMapValue = gson.toJson(mp);
editor.putString(HASHMAP_KEY, hashMapValue);
editor.apply();
}
private void restoreHashMap() {
initPreference();
String hashMapValue = sharedPreferences.getString(HASHMAP_KEY, null);
if(hashMapValue!=null) {
Type hashMapType = new TypeToken<HashMap<Integer, String>>(){}.getType();
Gson gson = new Gson();
HashMap<Integer, String> restoredHashMap = gson.fromJson(hashMapValue, hashMapType);
mp.putAll(restoredHashMap);
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
restoreHashMap();
}
@Override
protected void onDestroy() {
saveHashMap();
super.onDestroy();
}
}