Storing multi user data with just a username as the key using shared preferences

Viewed 18

I am trying to log a couple of fields of data per user and once the data is entered I want to be able to access it by entering the previously created username using shared preferences but I am not sure how to uniquely call a different shared preference when a username is entered ?

See some of my code below which just takes input of a username and two fields and allows to clear or retrieve the data

I want this feature but also the ability to enter a username which has been created here and then pull up the data which was wrote to the fields associated with that username

NOTE I want to keep the data local and not use an external database

  public class MainActivity extends Activity {
    // Shared Preferences
    SharedPreferences User1;
    SharedPreferences User2;` 



    private String userID;

   // Form Variables
    EditText name1;
    EditText name2;

    EditText Field1;
    EditText Field2;


    public static final String User1Pref = "User1pref";

    public static final String Name1 = "nameKey1";


    public static final String Field1Total = "Field1Key";
    public static final String Field2Total = "Field2Key";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name1 = (EditText) findViewById(R.id.Username1);
        Field1 = (EditText) findViewById(R.id.FieldTotal1);
        Field2 = (EditText) findViewById(R.id.FieldTotal2);

        User1 = getSharedPreferences(User1Pref,
                Context.MODE_PRIVATE);
        if (User1.contains(Name1)) {
            name1.setText(User1.getString(Name1, ""));
        }
        if (User1.contains(Field1Total)) {
            Field1.setText(User1.getString(Field1Total, ""));

        }
        if (User1.contains((Field2Total))) {
            Field2.setText(User1.getString(Field2Total, ""));
        }

    }


    public void Save(View view) {

        String n1 = name1.getText().toString();

        String f1 = Field1.getText().toString();
        String f2 = Field2.getText().toString();

        SharedPreferences.Editor editor = User1.edit();
        editor.putString(Name1, n1);
        editor.putString(Field1Total, f1);
        editor.putString(Field2Total, f2);

        editor.commit();
    }

    public void clear(View view) {

        name1 = (EditText) findViewById(R.id.Username1);

        Field1 = (EditText) findViewById(R.id.FieldTotal1);
        Field2 = (EditText) findViewById(R.id.FieldTotal2);

        name1.setText("");

        Field1.setText("");
        Field2.setText("");

    }

    public void Get(View view) {
        name1 = (EditText) findViewById(R.id.Username1);

        Field1 = (EditText) findViewById(R.id.FieldTotal1);
        Field2 = (EditText) findViewById(R.id.FieldTotal2);

        User1 = getSharedPreferences(User1Pref,
                Context.MODE_PRIVATE);

        if (User1.contains(Name1)) {
            name1.setText(User1.getString(Name1, ""));
        }
        if (User1.contains(Field1Total)) {
            Field1.setText(User1.getString(Field1Total, ""));

        }
        if (User1.contains(Field2Total)) {
            Field2.setText(User1.getString(Field2Total, ""));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}
0 Answers
Related