[TUTORIAL] MultiCheckableListView (by ids)

Hi guys, this time I want to furnish you a tutorial about ListView, SqliteOpenHelper and in particular an extension about CheckedView.

We’ll obtain a result like in these pictures and don’t worry if you see ICS: it’s backward compatible from android 2.2 Froyo.

This slideshow requires JavaScript.

(You can download this tutorial here)

As you can see in those pictures, we have got an Activity that allows user to fill a ListView with contacts names and pictures. At the same time user can clear it and check some of them.

The first of our steps is the creation of the database that will hold our data and the implementation of a basic DAO for our contacts.

So in the project under the path “it.trento.alchemiasoft.casagranda.simone.db” you’ll find the implementation of two classes:

  • Contact: bean representation of a contact and DAO implementation to perform some basic operations on Cursors and Database.
  • OpenHelper: an extension of the android SqliteOpenHelper that creates our Contacts Table performing an SQL statement.

Then we can prepare our <layout>.xml for our views:

  • pick_contacts_container.xml: it’s a RelativeLayout that contains two Buttons to clear and fill list, a particular ListView and an header with a title
  • simple_checked_contact_list_item.xml: another RelativeLayout that contains an ImageView to hold contact picture, and a particular CheckedTextview to keep selection state and hold contact name

Pay attention: I have said “particular” because as you can see the listview and empty label refer to id that the framework uses in a ListActivity to identify a listview and an empty label (so you can write your own layout instead of use the just inflated ones). Our list_item contains a CheckedTextView that has is checked symbol that refers to the one used by android.R.layout.simple_list_item_multiple_choice.xml

The last step of this tutorial is the implementation of our Activity that will manage next points:

  • Creation and maintenance of the application database
  • Data loading
  • Data persistence (including user selection to avoid losses after screen-orientation changes, …)

In TutorialActivity you’ll find this activity that is well commented and that allows you to understand step by step how I had built its life cycle and the related events.

The most important feature of the Adapter set into the ListView is that it uses “the database table _id field” to refers to list_item_views, instead of the position.

I have used an HashSet of Long to that and to keep trace of the inserted elements after rotation I store/restore these elements in the bundle with whom your Activity keeps its state.

If you have got doubts or if you want to thank me ( 😛 ) don’t hesitate to write me an email or under this post.

See you,

Simone

Advertisements

About alchemiasoft

Android Developer at Bloomberg LP. Algorithms and performance passionate. Curious. Eager to learn.

Posted on March 30, 2012, in Android and tagged , , , , . Bookmark the permalink. 5 Comments.

  1. I simply want to mention I’m all new to blogging and site-building and certainly liked this page. Probably I’m want to bookmark your site . You definitely come with wonderful stories. Cheers for revealing your web site.

  2. I enjoy studying and I believe this website got some really utilitarian stuff on it! .

  3. You could definitely see your expertise within the work you write. The arena hopes for even more passionate writers like you who are not afraid to say how they believe. All the time follow your heart.

  4. I got what you intend, regards for posting .Woh I am pleased to find this website through google. “It is a very hard undertaking to seek to please everybody.” by Publilius Syrus.

  1. Pingback: Tutorial android - Pagina 4 - Forum Android Italiano

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: