Creating a simple shopping list android App

Hello, this week I decided to take a break of the game programming stuff so in this post I’m going to describe how to build a basic shopping list app that could help you get more familiar with the android API and make use of the following features that android provides:

  • Touch gestures
  • SQLite database
  • Customized ViewGroup

The idea of this app is that when I run out of something I will mark an item as “not available” and then after I go to the grocery store I will check it again as available. Whenever an item is marked as available or not the status is saved in the local db immediately.

Here is an screenshot of the app:

screenshot

  • The SlideToggle

I wanted to have a “switch toggle” like user control to indicate if an item is available or not using a swipe gesture so I created one using a Custom ViewGroup.

animateddemo

The viewgroup is composed of two imageviews at each side and in the middle 2 textviews to display the description of the item. In the next figure you can see the layout and the order at which the TouchEvents are called to achieve the swipe gesture.

customviewgroup

 

Since loading images can be quite slow it is better to use an AsyncTask in order to improve the scrolling response of the listview by not blocking the UI thread.

new AsyncTask<Object, Void, Drawable>() {
ImageView imv1 = null;
ImageView imv2 = null;


@Override
protected Drawable doInBackground(Object... params) {
String imageName = (String)params[0];
Drawable d= getImageFromAssets(imageName);

imv1 =  (ImageView)params[1];
imv2 =  (ImageView)params[2];

return d;
}

@Override
protected void onPostExecute(Drawable d) {
super.onPostExecute(d);
if (d!= null) {
imv1.setImageDrawable(d);
imv2.setImageDrawable(d);
}
}

}.execute(o.getPictureName(),cr.mImageLeft, cr.mImageRight);

  • The sqlite database

I’m using a simple table that contains the list of items


CREATE TABLE TABLE_ITEM_CATALOG (    

COLUMN_ITEMID  INTEGER PRIMARY KEY AUTOINCREMENT,

COLUMN_NAME  TEXT NOT NULL,

COLUMN_DESCRIPTION TEXT NOT NULL,

COLUMN_IMAGENAME TEXT NOT NULL,

COLUMN_STATUS  INTEGER NOT NULL )

To work with sqlite in android you need to create a class that extends the SQLLiteOpenHelper class. The very first time you request access to the database and it doesn’t exist yet the method DBHelper.onCreate will be called so you can create the database tables and provide initial data.


public void onCreate(SQLiteDatabase database) {
...
...
database.execSQL(DATABASE_CREATE);

  • Data access model

In order to update the database I setup a basic Data Access Object Model with generics. This data access object model operates over DTOs (Data Transfer Objects) and in this case there is just one dto class named “item”.

DaoStructure

When the switch toggle for an item is updated the OnChange callback is triggered and at this point I call the dao’s update method to update the item in the database.

DtoStructure

this.m_adapter = new ItemListAdapter(

  this.getActivity(), R.layout.row, m_dao.getList());

this.m_adapter.setOnItemViewChangeListener(

       new ItemListAdapter.OnItemViewChangeListener()

       { // Update the item status in the DB table

        @Override

        public void onChange(Item item) { m_dao.update(item);

        }

      });

That’s it you can download the source code from github.

Advertisements
Tagged

3 thoughts on “Creating a simple shopping list android App

  1. Nice app…!! Please provide us the graphical layouts of your app

  2. Saikat says:

    please give me the source code of this application
    here’s my email address

    saikatuoda@gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: