0

Just wondering how to do this on Android. My application needs to store a list of the user's accounts. Each account would have, account name username, password, server address etc.

I first tried to implement this with Preference Activity, this worked well but it seems to only result in a user interface for a single account. I am missing how to arrange this so the data is stored for an array of accounts, so if Account 3 is selected from a top level list the Preferences will display the settings for Account 3.

For example if you have an email app with multiple accounts, you want to be able to configure each account individually. They have the same settings, but different instances, so each account would have it's own preference file.

Thanks

tech74
  • 301
  • 5
  • 20

1 Answers1

1

You will most likely need a database (local sqlite usually) to store the data then you will have you use either the ListView and implement onClick methods OR as you say the PreferenceScreen and add preferences programmatically when you retrieve your data from the database for each account. In order to achieve it take a look here Dynamic ListPreference in android or here How can I keep on adding preferences when i click one?


hope this helps abit

Community
  • 1
  • 1
Sergey Benner
  • 4,421
  • 2
  • 22
  • 29
  • Shared preferences i think are fine for storing the data rather than SQLLite. Its just how do i use these shared preferences to populate a preference activity and vice versa. Hope its clear – tech74 Feb 07 '12 at 10:47
  • depends on the data type. if you want the data to be related later like e.g. game data rounds,scores for users or users addresses for another type of application. you would want this data to be normalized and store it in a relational storage. that's my point. – Sergey Benner Feb 07 '12 at 11:37
  • yes. the database is the answer here. you have an account + settings bound either to one general setting or each can have its own set of settings. here http://stackoverflow.com/questions/3233429/managing-multiple-sharedpreference-files and here http://stackoverflow.com/questions/7420633/android-how-many-sharedpreferences-files they all advice on using database – Sergey Benner Feb 07 '12 at 12:13
  • Ok, but does this mean I can still use the Preference Activity screen or do i need to create my own. Reason I am asking is the settings shown to the user will need to be read from the database in your solution. But the Preference Activity plus other OS classes all seem to update the SharedPreferences object. – tech74 Feb 07 '12 at 12:27
  • Re-phrasing comment. You will have to create your own PreferenceActivity but it is possible to use with the database still. You can also create your own Preferences activity using ListViews and/or other widgets. If you want to stick with standard PreferenceScreens there's another interesting link to that matter http://stackoverflow.com/questions/7500926/is-it-possible-to-use-preferenceactivity-with-sqlite-instead-of-res-xml – Sergey Benner Feb 07 '12 at 13:00