简体   繁体   English

如何使用sharedpreference在listview中保存项目

[英]How to save item in listview using sharedpreference

I am just learning android studio. 我正在学习android studio。

then, I want to save my list item in my listview 然后,我想将列表项保存在列表视图中

only using shared preference. 仅使用共享首选项。

I succeeded to Add and save item. 我成功添加并保存了项目。

but it can save only one row! 但只能保存一行!

then I input any key. 然后我输入任何键。 it just changes key before adding the item. 它只是在添加项目之前更改键。

so, how to adding list and save ? 那么,如何添加列表并保存呢? ! please help me 请帮我

enter code here

// listview save sharedpreference
private void savePreferences () {
    String ten = editTen.getText().toString();
    String sdt = editSDT.getText().toString();
    SharedPreferences pref = getSharedPreferences("pref", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString("list1", ten);
    editor.putString("list2", sdt);
    editor.commit();
}

// load sharedpreference //加载sharedpreference

private void getPreferences() {
        SharedPreferences pref = getSharedPreferences("pref", Context.MODE_PRIVATE);
        String date = pref.getString("list1", null);
        String name = pref.getString("list2", null);
            arrSinhVien = new ArrayList<SinhVien>();
            arrSinhVien.add(new SinhVien(R.drawable.luan, date, name));
            myadapter = new CustomAdapter(this, R.layout.activity_diary_item, arrSinhVien);

    }

The Shared preferences is not use to save the list of items, Its just key value pair data structure to save small amount of data of your application. 共享首选项不用于保存项目列表,它只是键值对数据结构,可以保存应用程序的少量数据。
In your snippet you overwrite the previous data with new one. 在您的代码段中,您将用新数据覆盖以前的数据。
But still if you want to save the list data, I suggest that to use the JSON array and put that Array into the shared preferences. 但是仍然要保存列表数据,我建议您使用JSON数组并将该数组放入共享首选项中。
The Code is like as follows. 该代码如下。

 JSONArray jsArr=new JSONArray(); JSONObject jsObj=new JSONObject(); jsObj.putString("ten",editTen.getText().toString()); jsObj.putString("sdt",editSDT.getText().toString()); //Add this object to your JSONArray jsArr.add(jsObj); //Serialize your Json Array and save to shared preferences. savePreferences(jsArr); 

private void savePreferences (JSONArray jsArr) {
SharedPreferences pref = getSharedPreferences("pref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("list", jsArr.toString());
editor.commit();
}

And use this as follows 并如下使用

private void getPreferences() {
    SharedPreferences pref = getSharedPreferences("pref", Context.MODE_PRIVATE);
    arrSinhVien = new ArrayList<SinhVien>();
    String jsArrList = pref.getString("list", null);
    JSONArray jsArr=new JSONArray(jsArrList); 
    for(JSONObject obj:jsArr){
      String date=obj.getString("ten");
      String name=obj.getString("sdt");
      arrSinhVien.add(new SinhVien(R.drawable.luan, date, name));
    }
    myadapter = new CustomAdapter(this, R.layout.activity_diary_item, arrSinhVien);

}

Hope this will help you.. 希望这个能对您有所帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM