1

First - sorry for my english;) I'am writing profile manager for Android and i want getString from a few SharedPreferences file and create listView. It's part of my code:

private static final String PN = "profile_name";
private EditTextPreference editText;
private SharedPreferences preferences;

public class Profile_Preferences extends PreferenceActivity {

...

private void SavePreferences() {

    String text= editText.getText().toString();
    preferences = getSharedPreferences("Profile_" + text, Activity.MODE_PRIVATE);  //Here we created SharedPreferences file with name "Profile_"+ this what user write in editText

    SharedPreferences.Editor preferencesEditor = preferences.edit();
    preferencesEditor.putString(PN, editText.getText());
    preferencesEditor.commit();

Ok. The user was doing a few profile, so we have file for example: Profile_home.xml, Profile_work.xml, Profile_something.xml. Now i wan't create listView with profile name from this file. It's my next Activity:

public class Tab_profiles extends ListActivity {

ArrayList<String> listItems = new ArrayList<String>();  
ArrayAdapter<String> adapter; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, listItems);
    setListAdapter(adapter);
 ...

public void LoadList() {        

 //It's a good idea to getString "profile_name" form every xml file with name "Profile_" + "" ? 
    preferences = getSharedPreferences("Profile_"+ "", Activity.MODE_PRIVATE);

    String take_profile_name = preferences.getString("profile_name", null);
    listItems.add(take_profile_name );
    adapter.notifyDataSetChanged();

}

But this doesn't work... logcat log:

FATAL EXCEPTION: MAIN 
java.land.Null.PointerException
at android.widget.ArrayAdaper.createViewFromResource(ArrayAdapter.java:355)
...

I don't now whoat it's wrong...

Please help my:) Thank you for any answers and sorry for my errors in writing and code;)

user1211477
  • 13
  • 1
  • 4

1 Answers1

2

There are a few problems in your code:

String text = editText.getText().toString();
preferences = getSharedPreferences("Profile_" + text, Activity.MODE_PRIVATE);

SharedPreferences.Editor preferencesEditor = preferences.edit();
preferencesEditor.putString(PN, editText.getText());

If a user types "home" in the EditText, you create a preference file called "Profile_home" and save its name in the same file? You need to save the user generated file names in a different file which has a name you know, so you can access it in your code.

Second problem:

preferences = getSharedPreferences("Profile_" + "", Activity.MODE_PRIVATE);
String take_profile_name = preferences.getString("profile_name", null);

You're trying to open "Profile_" settings. This file does not exist(?). The second parameter of getString must not be null, otherwise you'll add a null-reference to your list of string, which fails. Replace it with an empty string "".

Edit: Here's a little workaround to get all custom named preferences:

private void SavePreferences() {
    String text = editText.getText().toString();
    preferences = getSharedPreferences("ProfileNames", Activity.MODE_PRIVATE);
    SharedPreferences.Editor preferencesEditor = preferences.edit();
    // increment index by 1
    preferencesEditor.putInt("profile_count", preferences.getInt("profile_count", 0) + 1);
    // save new name in ProfileNames.xml with key "name[index]"
    preferencesEditor.putString("name" + (preferences.getInt("profile_count", 0) + 1), editText.getText());
    preferencesEditor.commit();
}

public void LoadList() {        
    preferences = getSharedPreferences("ProfileNames", Activity.MODE_PRIVATE);
    List<String> profileNames = new LinkedList<String>();
    int profileCount = preferences.getInt("profile_count", 0);
    for (int i = 1; i <= profileCount; i++) {
        profileNames.add(preferences.getString(name + i, ""));
    }
    listItems.addAll(profileNames);
    adapter.notifyDataSetChanged();
}
ottel142
  • 2,016
  • 1
  • 26
  • 37
  • Yes. You have right. This code isn't completed but first i wan't view listView with profile name. If i replece null in second parametr an empty string "" in getString, listView is displayed and logcat have no errors but listView doesn't displayed any values... – user1211477 Feb 15 '12 at 22:50
  • 1
    Your listView does not contain any values because the file Profile_.xml which you open in LoadList() does not contain a string with the key "profile_name", so "" is returned. It's e.g. Profile_USERINPUT.xml which contains "USERINPUT" for key "profile_name". – ottel142 Feb 15 '12 at 23:01
  • If we create some profile, example: "Home" we heave xml file "Profile_home.xml" and we have here key "profile_name". But how search all sharedPreferences xml file and view "profile_name" in listView? How search all files from the beginning "Profile_"? getSharedPreferences("Profile_" + "", Activity.MODE_PRIVATE) is bad? H – user1211477 Feb 15 '12 at 23:15
  • 1
    "Profile_" + "" = "Profile_", you just added an empty string which does not mean that you'll get all custom names. I added some code in my answer how you can save all custom names in one .xml (ProfileNames). Hope it helps. – ottel142 Feb 15 '12 at 23:43
  • 1
    Yor code and solution it's much better! Thank you very much for help and your time! Everything is now fine! You have a beer with me!!!:) – user1211477 Feb 16 '12 at 00:33