使用 SharedPreferences 时不获取唯一数据

Unique data isn't fetched when using SharedPreferences

我有一个 Activity 用户注册,在第二个 Activity 上,所有注册到现在的用户,信息将被显示。

我知道这可以使用数据库轻松完成,但我想使用 SharedPreferences。我正在使用 Gson 来 store/retrieve 数据。我有一个 class 学生,字段为 id(rollno),name.

我维护了一个用户数计数器,这个计数器将用于检索端。

将数据保存到共享首选项的代码:

 SharedPreferences preferences = 
    getSharedPreferences("mypref",MODE_PRIVATE);

        SharedPreferences.Editor prefsEditor=preferences.edit();
        Gson gson=new Gson();

        if(preferences.contains("counter")){
            counter=preferences.getInt("counter",0);
            prefsEditor.putInt("counter",++counter);

        }
        else{
            counter=0;
            prefsEditor.putInt("counter",++counter);
        }

        String json = gson.toJson(student);
        prefsEditor.putString("details"+counter,json);
        prefsEditor.commit();

        startActivity(intent);
}

检索代码 SharedPreference:

  SharedPreferences mPrefs = 
                    getSharedPreferences("mypref",MODE_PRIVATE);
    int howMany=mPrefs.getInt("counter",0);
    Gson gson = new Gson();
    for(int i=1;i<=howMany;i++) {
        String json = mPrefs.getString("details" + howMany, "");
        Student student = gson.fromJson(json, Student.class);
        Log.e("tag","loop number"+i);
        Log.e("Tag", student.getName());
    }

应用程序启动时,我的第一个用户名是 "Pra" 并且在第一个 Activity 上单击了提交按钮,因此它存储在 SharedPreferences.

第二个用户的名字是 "Suv",这个值也存储在 SharedPreferences 中。现在的问题是第一个用户的名字的值也发生了变化。我不明白。

我维护了计数器,以便每个对象在 SharedPreferences 中都有唯一的条目,但日志显示:

05-23 10:45:48.208 18409-18409/com.example.com.applicationstudent 
E/tag: counter: 2
05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent 
E/tag: loop number1
05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent 
E/Tag: suv
05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent 
E/tag: loop number2
05-23 10:45:48.212 18409-18409/com.example.com.applicationstudent 
E/Tag: suv

再说一遍,我不想用数据库,我想用SharedPreferences

您需要将数据以 JSON 数组格式保存到 SharedPreference 中。您总是需要将以前存储的数据获取到 ArrayList 中,当您单击提交时,然后将其添加到 Array List 对象中而不是新对象中。 因此先前的值不受影响并保存在数组列表中的下一个计数器。并保存到相同的 SharedPreference 键值中。

您可以使用 GSON 库轻松完成..

将数据保存到首选项

preferenceUtils.putStringPreferences(Constants.HISTORY_PREF, new Gson().toJson(historyList));

从首选项中检索数据

historyList = new Gson().fromJson(preferenceUtils.getStringPreferences(Constants.HISTORY_PREF),
            new TypeToken<List<History>>() {
            }.getType());

这里historyList是一个'History'类型的列表

请尝试修正读取逻辑,

 for(int i=1;i<=howMany;i++) {
    String json = mPrefs.getString("details" + howMany, "");
    Student student = gson.fromJson(json, Student.class);
    Log.e("tag","loop number"+i);
    Log.e("Tag", student.getName());
}

在 mPrefs.getString 中使用 "i" 而不是 "howMany"。由于 "howMany" 的值在整个循环中将保持不变,因此将获取相同的记录。

 for(int i=1;i<=howMany;i++) {
    String json = mPrefs.getString("details" + i, "");
    Student student = gson.fromJson(json, Student.class);
    Log.e("tag","loop number"+i);
    Log.e("Tag", student.getName());
}