Wednesday, January 12, 2011

Create List without extending ListActivity

Creating list view in Android is an easy task.
All we have to do is extending ListActivity, implement the methods and we are good to go.
But there are times when it is not an option, because we need to extend another class.
Let say....MapActivity? So we need to create a two tabbed view for user to be able to change from map view to list view. Our intended application may look like this.

When user click on an entry in list view, user will be redirected to the entry location in map view.
In this tutorial, we will create only a ListView then, we will add Tabs and MapView Tab.
We will need to create our implementation of ArrayAdapter and override its getView() method to accomodate our own view. We will create a title, a description and an icon for each row.

Dont forget to add uses library and internet permission in AndroidManifest.xml

First, we need to create a model class. Let say Restaurant.java



After that, we will create our view: main.xml



Next, create our layout for each row in our list view: row.xml



Next step, our main application view: DoubleTabView.java



Execute the application and we will see our generic list view application



Next, we will create a tab bar for user to choose List View or Map View and we will add the feature to navigate user to the map view whenever he/she click on a row in the List View

First, we should change our main.xml. We add TabHost, TabWidget and MapView



Next, we add the tab name programatically, then we add navigateToLocation method to navigate and zoom to the restaurant location



Build and run.
Our final application should look like this
Click one restaurant, and the application will navigate and zoom to the location

That's it! Have fun!





4 comments: