0

I'm writing a simple android app which needs to store 2 childrens details, i.e. name, age, toy. I was planning to do this using sharedpreferences. Here's my xml:

prefs.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
  <PreferenceScreen android:title="Edit Child Details">
    <PreferenceScreen android:title="Edit Child 1 Details">
      <EditTextPreference android:title="Name" android:dialogTitle="Name of Child 1" android:key="name1" />
      <EditTextPreference android:title="Age" android:dialogTitle="Age of Child 1" android:key="age1" />
      <EditTextPreference android:title="Toy" android:dialogTitle="Toy of Child 1" android:key="toy1" />
    </PreferenceScreen>
    <PreferenceScreen android:title="Edit Child 2 Details">
      <EditTextPreference android:title="Name" android:dialogTitle="Name of Child 2" android:key="name2" />
      <EditTextPreference android:title="Age" android:dialogTitle="Age of Child 2" android:key="age2" />
      <EditTextPreference android:title="Toy" android:dialogTitle="Toy of Child 2" android:key="toy2" />
    </PreferenceScreen>
  <ListPreference android:entries="@array/child_array" android:entryValues="@array/child_array" android:key="child_selector" android:title="Select Child"/>
</PreferenceScreen>

strings.xml
<string-array name="child_array">
      <item >1</item>
      <item >2</item>   
</string-array>

I know this could be better in sqllite database or local storage, but its only a basic app and I'm still a beginner.

What I'm trying to do in the code is then query the child_selector (i.e. user will select either child 1 or 2) preference and then I read the respective childs details for my app.

Not too sure if this is the best way to do things, so any advice would be welcome as I'm unsure how to reference the childs details. I'm have some basic unix scripting skills and I know this isn't an array, but not sure if its even feasable to use the java equivalent of ${string}[$number]

bazza2000
  • 131
  • 1
  • 12

1 Answers1

1

I am not really sure what your question is but if you don't need a preference screen but only the sharedpreferences you can do this too. You can save directly to the shared preferences like this:

// Retrieve an editor to modify the shared preferences.
SharedPreferences.Editor editor = mySharedPreferences.edit();
// Store new primitive types in the shared preferences object.
editor.putBoolean("isTrue", true);
editor.putFloat("lastFloat", 1f);
editor.putInt("wholeNumber", 2);
editor.putLong("aNumber", 3l);
editor.putString("textEntryValue", "Not Empty");
// Commit the changes.
editor.commit();

Hope it helps you


UPDATE

Shared preferences it the easiest way to save some quick data. alternatively you can also save to a file but I would not recommend this. And the database... it requires a lot of code lines compared to shared preferences.

So if it is just for a quick test I would use shared preferences.

If you don't need the data to be saved on application exit you can also just create a list of your own child class. But then the data will only be available until you close the application.

BR

7heViking
  • 7,137
  • 11
  • 50
  • 94
  • Sorry, that's probably my fault for not being very descriptive. What I'm looking for really is a way to store kids details and then select one of them for use in the application. Thought preferences might be an easy way, but can't work out how to actually use them. – bazza2000 Jul 26 '12 at 19:16