简体   繁体   中英

How do I display all saved shared preferences in a listview using phone number as the key?

I am building an app that asks for contact information, such as name,email, phone number, city, state and zipcode.

I am learning about SharedPreferences, and trying to experience it to the fullest extent. I do not want to use a SQLite database yet, I know that it would be better for my application but I am trying to use sharedpreferences right now.

Basically I have the contact information coming from a few edittext fields, which are then assigned to a grouped string, that includes all of the above attributes separated by a comma as a delimiter. Lets say I call this ContactInstance

I am trying to use the string I pull from the edittext for the phone number as the key for each sharedpreference instance saved. so for instance if someones phone number was 521-345-3456 then this value would be assigned to ClientPhoneNumber, and then when I added a shared preference key-value pair it would look like this:

editor.putString("clientNum", ClientPhoneNumber);
editor.putString(ClientPhoneNumber, ContactInstance);

Is this correct?

Then to retrieve ALL of the values, I would probably need to use a list view but I dont know how to display them. Currently I can get it to display ONE item, but all values are "null" separated by the delimiter.

To retrieve, would this work?

SharedPreferences sharedPreference = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String ClientPhoneNumber = sharedPref.getString("clientNum", "");
String ContactInstance = sharedPref.getString(ClientPhoneNumber, "");

Is that the correct way to do it? it's not working as of now so not sure where I am going wrong.

I Think that

String ContactInstance = sharedPref.getString(ContactInstance, "");

Should be

String ContactInstance = sharedPref.getString(ClientPhoneNUmber , "");

EDIT:

This code Works for me: (Should work inside an Activity class)

private SharedPreferences getMySharedPreferences() {
    return getSharedPreferences(MainActivity.class.getSimpleName(),
            Context.MODE_PRIVATE);
}

private void storeData(String data) {
    final SharedPreferences prefs = getMySharedPreferences();
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("data", data);    
    editor.commit();
}

private String retrieveData() {
    final SharedPreferences prefs = getMySharedPreferences();
    return prefs.getString("data", "");    
}

NOTE: MainActivity should be your MainActivity class name.

EDIT2:

If you want store a LIST, SharedPreferences is no the better way . But there are a way.

private void storeRecord(String Id, String Name, String Phone, String Address) {
    final SharedPreferences prefs = getMySharedPreferences();
    SharedPreferences.Editor editor = prefs.edit();
    String data = TextUtils.join(",", new String[]{Id, Name, Phone, Address});
    editor.putString("record_" + Id, data);
    editor.commit();
}

private String[] getRecord(String Id, bool usePrefix) {
    final SharedPreferences prefs = getMySharedPreferences();
    String data = (String) prefs.getAll().get((usePrefix ? "record_" + Id : Id));
    return TextUtils.split(data, ",");
}

private ArrayList<String[]> getAllRecords() {
    final SharedPreferences prefs = getMySharedPreferences();
    Map<String,?> all = prefs.getAll();
    Iterator it = all.entrySet().iterator();
    ArrayList<String[]> result = new ArrayList<String[]>();
    while(it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        if (pairs.getKey().toString().startsWith("record_")) {
            result.add(getRecord(pairs.getKey().toString(), false));
        }
    }
    return result;
}

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