简体   繁体   中英

android shared preferences seem to be overwritten Java

I am pretty new to the android programming thing so I am sure that there is something simple that I am not doing correctly. I have two activities that are accessing a shared preferences file; one that reads from it and displays the contents to a list, and the other reads the contents and adds a new item to the list based upon user input. The issue that I am having: when I read the contents from the file and attempt to add a new item to the list my original list is being overwritten. Maybe that's not what is actually happening but it seems to me that this is the issue. When I add a new item from the child activity and return to the original activity which displays the list I am only seeing the newest item that I saved and not any other items that I had created previously.

The first activity is the main activity and it simply reads the shared preferences and adds the items in a string, separated by commas and displays them in a listview. I only showed the method for displaying the items in the listview:

    public void addItems() {
    String strLists="";
    SharedPreferences sharedPref = getSharedPreferences("ListNamesFile", MODE_PRIVATE);
    strLists = sharedPref.getString(getString(R.string.edit_message), strLists);
    listItems.clear();
    String[] myLists = strLists.split(",");
    for(int r=0;r<myLists.length;r++){
        listItems.add(myLists[r].toString());
    }
    //test adding an extra list item to make sure that the issue isn't from adding 
    //items to the listview
    //listItems.add("test");
    adapter.notifyDataSetChanged();
    }

The second activity is for getting a new string from an edit text control, adding it to the end of the string with the list items, and committing the changes to the shared preferences file. I have included the method for retrieving the shared preferences and writing the new item to the end of the string:

    public void createListOnClick(View view){
    //create a file for storing list items
    SharedPreferences sharedPref = getSharedPreferences("ListNamesFile", MODE_PRIVATE);
    String strMyLists = "";
    sharedPref.getString(getString(R.string.edit_message),strMyLists);
    String[] lists = strMyLists.split(",");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < lists.length; i++) {
        sb.append(lists[i]);
        sb.append(",");
    }
    SharedPreferences.Editor editor = sharedPref.edit();
    EditText txtListName = (EditText)findViewById(R.id.txt_list_name);
    sb.append(txtListName.getText().toString());
    editor.putString(getString(R.string.edit_message), sb.toString());
    editor.commit();
    finish();
    }

It seems that it must be something simple that I am missing but I haven't been able to figure it out so any help would be greatly appreciated.

您仍然没有在strMyLists中分配sharedPref值。

In the second activity, you're not actually assigning the read shared preferences:

You should replace

sharedPref.getString(getString(R.string.edit_message),strMyLists);

with

strMyLists = sharedPref.getString(getString(R.string.edit_message),strMyLists);

in getString method

sharedPref.getString(getString(R.string.edit_message),strMyLists);

second argument is default value. So you will have to write.

strMyLists = sharedPref.getString(getString(R.string.edit_message),"");

it will work with this. rest of the code is good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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