1

How can I go about storing user input and making a list using sharedpreferences

I figured you would need an array of some sort, just not sure how to go about it. Here is some code I have.

MainActivity class:

 public void SavePreferences (View view){
    SharedPreferences prefer = getSharedPreferences("sharedPrefs",MODE_PRIVATE);
    SharedPreferences.Editor prefEditor = prefer.edit();
    EditText editLName = (EditText) findViewById(R.id.editLName);
    EditText editFName = (EditText) findViewById(R.id.editFName);

    List<String> user = new ArrayList<String>();
    List<String> pass = new ArrayList<String>();

    userList.add(editFName.getText().toString());
    passList.add(editLName.getText().toString());

    for(int i = 0; i < userList.size(); i++){
        prefEditor.putString("user_" + i, user.get(i));
        prefEditor.putString("pass_" + i, pass.get(i));
    }
    prefEditor.commit();

}

Made some changes to the MainActivity just so I can illustrate better what I'm trying to achieve. I think one of the issues here is that this would overwrite any existing users, and I still don't have a means of retrieving the list.

SavedPreferences Class:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.savedview);
    SharedPreferences prefer = getSharedPreferences("Prefs",MODE_PRIVATE);


    TextView txtMulti = (TextView) findViewById(R.id.txtMulti);


    txtMulti.setText(list);
}
4
  • Forget about Preferences make one Static Arraylist and append values on it and fetch it.
    – M D
    Jan 13, 2015 at 9:47
  • What you want to do actually? Is the user input is permanent?
    – Vilas
    Jan 13, 2015 at 10:14
  • Yeah that is the idea Jan 13, 2015 at 10:15
  • workaround(quite literally too): you can make an ArrayList as per your need and save the toString() value in SharedPreferences, retrieve String, for-each back to the list
    – Pararth
    Jan 13, 2015 at 10:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.