Skip to content Skip to sidebar Skip to footer

Using Preferences -- My Listview Is Empty (can't Store Data)

I need help in order to properly set up preferences. I have my main activity from which by pressing the menu button ,i am going to the preferences activity.There,i have 3 entries

Solution 1:

Try this....

SharedPreferences mypref= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefsEditr=mypref.edit();
prefsEditr.putString("username", username);
prefsEditr.commit();

username = mypref.getString("username", "");

Solution 2:

1): In prefsEditr.putFloat("strength", -1); the value "-1" is an Integer and therefore stored as an Integer. You could use Float.parseFloat to parse an Integer (eg parsing the stored Integer value or parsing an Integer value and store it).

Can you please elaborate 2) and 3)? I did not get what exactly you meant.

/edit: Some sample code for saving / reading an array

publicString[] readArray() {
    SharedPreferences openPreferences = context.getSharedPreferences("arrayfile", Context.MODE_PRIVATE);
    finalInteger counter = openPreferences.getInt("size", 0);
    String[] result = newString[counter];
    for (int i=0;i<counter;i++) {
        result[i] = openPreferences.getString("entry_"+i, "");
    }
    return result;
}

publicvoid writeArray(String[] array) {
    Editor editor = context.getSharedPreferences("arrayfile", Context.MODE_PRIVATE).edit();
    finalInteger counter =array.length; 
    editor.putInt("size", counter);
    for (int i=0;i<counter;i++) {
        editor.putString("entry_"+i, array[i]);
    }
    editor.commit();
}

/edit: Also use a PreferenceActivity instead of a ListActivity. That makes things easier.

Then add this code in OnCreate():

PreferenceManagerlocalPreferenceManager= getPreferenceManager();
    localPreferenceManager.setSharedPreferencesName("main_prefs");
    localPreferenceManager.setSharedPreferencesMode(MODE_PRIVATE);
            [for every entry do:] {
                addNewPref("ArrayValue"); 
            }

and then add dynamically Preference Views to the Activity, just adjust the code below:

privatevoidaddNewPref(String title) {
    Preference newPref = newPreference(this);
    newPref.setTitle(title);
    ((PreferenceScreen) getPreferenceManager().findPreference("category_key")).addItemFromInflater(newPref);
}

Content of the PreferenceScreen XML:

<?xml version="1.0" encoding="utf-8"?><PreferenceScreenandroid:key="category_key"xmlns:android="http://schemas.android.com/apk/res/android" ></PreferenceScreen>

Solution 3:

So, it seems like you're able to pass the serial numbers over to your list activity, and they show up in the list, right? And now you're wondering what to do with the adapter?

If so, then you just need to override onListItemClick, pull the string from values[position], store that back into your preferences, and finish() the list activity. Make sure turn that String[] into a field instead of a local var, so that you can fill it in in onCreate and use it on onListItemClick.

String[] listItems = null;//fill in in onCreateSharedPreferencesprefs=null;//grab in onCreate@OverridepublicvoidonListItemClick(ListView l, View v, int position, long id)
{
    Log.i(TAG, "position: " + position);
    Log.i(TAG, "item: " + listItems[position]);
    prefs.edit().putString("mySerial", listItems[position]).commit();
}

Solution 4:

your array is empty.

change this:

for (int i=0;i<counter;i++) {
    prefsEditr.putString("serial_number"+i, values[i]);
}

with:

for (int i=0;i<counter;i++) {
    // just replace the string with you serial number;
    values[i] = "998-123-234-122_" + i;
    prefsEditr.putString("serial_number"+i, values[i]);
}

Post a Comment for "Using Preferences -- My Listview Is Empty (can't Store Data)"