使用SharedPreferences

时间:2018-05-23 05:27:16

标签: android gson sharedpreferences android-sharedpreferences

我有一个Activity用户注册,在第二个Activity,所有注册到目前为止的用户都将显示该信息。

我知道这可以使用数据库轻松完成,但我想使用SharedPreferences。我正在使用Gson来存储/检索数据。我有一个班级为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());
    }

当应用程序启动时,我的第一个用户的名字是&#34; Pra&#34;并在第一个Activity上点击了提交按钮,以便将其存储在SharedPreferences中。

第二个用户的名字是&#34; Suv&#34;这个值也存储在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

3 个答案:

答案 0 :(得分:0)

您需要将JSON数组格式的数据保存到SharedPreference中。总是需要将以前存储的数据存入ArrayList,当您单击提交时,则添加到Array List对象中而不是新对象中。 所以之前的值没有影响并保存在Array List的下一个计数器中。并保存到相同的SharedPreference键值。

答案 1 :(得分:0)

您可以使用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是历史记录&#39;型

答案 2 :(得分:0)

请尝试更正读取逻辑,

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

使用&#34;我&#34;在mPrefs.getString而不是&#34; howMany&#34;。作为&#34; howMany&#34;的价值在整个循环中将保持不变,将获取相同的记录。

 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());
}
相关问题