Blog Archives

[TUTORIAL] Android expansion and compression of items in a ListView. #AndroidDev


When I started to develop the first Android version of MyNextBus in London, I had to think about how implement the section management of different items types into a list. It was a great opportunity, at the time of the first writing, to use the framework’s widget called ExpandableListView. If you have never heard about that before, a quick look should solve any lack and make you more comfortable with what you’ll read below. Read the rest of this entry


[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,