How to add data dynamic on recyclerView and save the data?

后端 未结 2 1161
执念已碎
执念已碎 2020-12-22 06:21

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

相关标签:
2条回答
  • 2020-12-22 06:39

    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 :)

    0 讨论(0)
  • 2020-12-22 06:49

    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

    0 讨论(0)
提交回复
热议问题