Uses Classes: ListActivity, SQLiteOpenHelper, SQLiteDatabase, CursorAdapter, PreferenceActivity
I finally decided to make this staple app way later than I should have. There really isn't anything new in this app just reenforcing things I've already learned. I did make some effort to at least write down steps to implement the common patterns in this app that I would use again.
- Local SQLiteDatabase for persistent storage
- Define one or more Contract classes. You could do one Contract for a number of tables or one contract for every table. It's up to you how you organize it. You just need to know where things are.
- Define a static Entry class, which implements BaseColumns, for every table you will use in your database.
- Extend SQLiteOpenHelper and override onCreate and onUpgrade methods and define a constructor. The onCreate method will call db.execSQL on a string containing your create table query.
- Define static CRUD methods for each action you'll be taking on the database. Make these methods of the Contract class so you can access them from anywhere (Activity, Adapter, etc.)
- Create a ListView with android:id="@android/id:list" in your main activities layout.
- Create a layout file layout/list_item_layout.xml.
- Extend CursorAdapter in a new class defining a constructor and overriding the newView and bindView methods. In new view inflate R.layout.list_item_layout and return it.
- Optionally, create a static private class called ViewHolder. In here define public field variables for each of the views in your list item's layout. Define a constructor that requires a view as a parameter and using that view's findViewById method assign your field variables to these views for easy access later. In your overridden newView method you instantiate a ViewHolder and call setTag on the view which you return. When you implement bindView call getTag on the view and cast it to the ViewHolder type to access the view identifiers.