2

I am using a custom List adapter to display a list of all contacts, and the user will then select contacts by clicking a checkbox. What is a good method of saving the selected contacts to preferences, so that I may test against them elsewhere? I was thinking of using SharedPreferences, but so far I have been unable to find a way to save an array to SharedPrefs. I could go the sqlite route, but that seems a bit excessive considering they are already contained in a db, and why couldn't I just reference them there. Just not sure how to even begin to do that... Also, I was thinking of calling this method onDestroy, though that could have some issues as well, so a recommendation on that could help as well. Here is some code (let me know if you need more,,,there's always more) thanks for your supreme knowledge.

ListItemLayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/relativelayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@android:id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@android:id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/text1"
            android:includeFontPadding="false"
            android:paddingBottom="4dip"
            android:textSize="15sp"
            android:textStyle="normal" />

         <CheckBox
                android:id="@+id/checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:focusable="false"
                                 />
 </RelativeLayout>
</TwoLineListItem>

ContactPicker Activity (some code redacted for brevity's sake):

public class ContactPicker extends ListActivity implements Runnable{


        private List<Contact> contacts = null;
        private Contact con;
        private ContactArrayAdapter cAdapter;
        private ProgressDialog prog = null;
        private Context thisContext = this;
        private CheckBox c1;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            prog = ProgressDialog.show(this, "Contact List", "Getting Contacts", true, false);
            Thread thread = new Thread(this);
            thread.start();

            final CheckBox c1 = (CheckBox)findViewById(R.id.checkbox);

        }
public void run() {...
}
private List<Contact> fillContactsList() {...
}
 private Handler handler = new Handler() {...
}
@Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            TextView label = ((TwoLineListItem) v).getText2();
            String phoneNumber = label.getText().toString();
}
}
benbeel
  • 1,532
  • 4
  • 24
  • 38

2 Answers2

2

If all you need is to store a list of objects (users) I would skip the DB and just create a JSON object and store that as a String in a shared preference. It obviously depends on how many users you expect to store, but if it's a few hundred or less, you're not going to see any sort of performance loss, and you can avoid dealing with all the db setup.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
1

Yea, you're best route is to use a sql database. You only really have two options for persistent data storage in android: sqlite and shared preferences*. Since you want to actually store something more complicated a single key value pair, your best bet is to go with a sqlite database. As far as when to store the data, I would just trigger a store each time an item is checked or unchecked individually using an OnCheckChangedListener.

*Yes, there are other options, but those are the only semi-easy ones to use.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102