I'm currently creating an app in android studio where I store user input into a hashmap and show it into a listview, but when I close the app and open it back up, the data is gone and it shows an empty listview.
Is there a way to solve this?
Suppose you have a hashMap val mp = HashMap<Int, String>()
. Then you need to save the contents of this hashmap when your app closes, and restore data on app open. You can store the map inside sharedPreference
or preferenceDataStore
. I'll show you the sharedPreference way:
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()}")
}
Here, the function names are self explainatory. Simply save data when app closes:
override fun onDestroy() {
saveHashMap()
super.onDestroy()
}
And restore data on app open:
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...
}
Notice that in this example, I used HashMap<Int, String>. If you use map of other type, you should change the codes accordingly.
This is the java code can look like:
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.