I want to save the data when user input edit text.
It can show on recyclerView and the app can show it again when the app had restarted.
I try to user share
If you have a large amount of data, you need to have SQL. If you want to use shared preference, you may try this:
private void saveBlood(){
SharedPreferences preferences = getSharedPreferences("uerInputBlood", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
listData.clear();
editor.putInt("count", listData.size());
for(int i=0; i<listData.size(); i++){
editor.putString("blood"+i, listData.get(i));
}
editor.commit();
}
private void restoreBlood(){
listData.clear();
SharedPreferences preferences = getSharedPreferences("uerInputBlood", MODE_PRIVATE);
int tmpCount = preferences.getInt("count", 6);
for(int i=0; i<tmpCount; i++){
listData.add(preferences.getString("blood"+i, ""));
}
}
Hope it help :)
Try moving myAdapter.notifyDataSetChanged();
to the end of onResume()
. You don't need it inonCreate()
since you just created that adapter and the data did not change after.
Also, you don't need to call this part in both onCreate()
and onResume()
. Remove it from onCreate()
. You are just adding the same item twice to your list that way:
String getUserInputBlood = preferences.getString("uerInputBlood", "");
listData.add(getUserInputBlood);
SharedPreferences
are usually used to store a few key/value data items needed by your application. They are not intended for storing lists.
If you are trying to store all the list values, you should use some other form of data storage, e.g. a SQLite database. For more details, see storage options