关闭应用程序后,哈希图中的数据不会保存



我目前正在android studio中创建一个应用程序,我将用户输入存储在哈希图中,并将其显示在列表视图中,但当我关闭应用程序并重新打开时,数据不见了,它显示了一个空的列表视图。

有办法解决这个问题吗?

假设您有一个hashMapval mp = HashMap<Int, String>()。然后,您需要在应用程序关闭时保存此哈希图的内容,并在应用程序打开时恢复数据。您可以将地图存储在sharedPreferencepreferenceDataStore中。我将向您展示共享偏好方式:

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<Int,字符串>。如果您使用其他类型的地图,则应相应地更改代码。

编辑

这是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();
}
}

最新更新