Android: Dealing with ListActivities, customized ListAdapters and custom-designed items

Phillip Steffensen's picture

Due to its ListActivity class the Android SDK helps developers to create List-GUIs easily. Therefore the ListActivity uses a ListView. But what about customized lists? What about own list-designs? In this post I'll try to show how to extend a standard ListView to create a custom list-design. To create your own list-design, you should define how an item in your list should look like. As an example I'll use a list of Twitter-updates (also called „tweets“).

To define how a tweet should be displayed you can create a new layout-file (e.g. tweet.xml) in res/layout/. Its content could look like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TweetLayout" android:layout_width="fill_parent"
  3.    android:layout_height="wrap_content">
  4.     <TableLayout android:id="@+id/TableLayout" android:layout_width="wrap_content" android:layout_height="wrap_content">
  5.         <TableRow>
  6.             <TextView android:text="" android:id="@+id/TweetUserName" android:layout_width="wrap_content"
  7.                android:layout_height="wrap_content" android:layout_marginLeft="10px" android:textSize="5pt"></TextView>
  8.         </TableRow>
  9.         <TableRow>
  10.             <TextView android:text="" android:id="@+id/TweetText" android:layout_width="wrap_content" android:layout_height="wrap_content"
  11.                android:textStyle="bold" android:textSize="8pt" android:layout_marginLeft="10px"></TextView>
  12.         </TableRow>
  13.     </TableLayout>
  14. </LinearLayout>

This simple layout definition declares how a single tweet should be structured and displayed as an item in the list. To let the ListView use this layout a customized ListAdapter must be set. To create such an adapter the class BaseAdapter can be used.
  1. public class TweetListAdapter extends BaseAdapter {
  2.  
  3.     private List<Tweet> tweetList;
  4.  
  5.     private Context context;
  6.  
  7.     public TweetListAdapter(List<Tweet> tweetList, Context context) {
  8.         this.tweetList = tweetList;
  9.         this.context = context;
  10.     }
  11.  
  12.     public int getCount() {
  13.         return tweetList.size();
  14.     }
  15.  
  16.     public Tweet getItem(int position) {
  17.         return tweetList.get(position);
  18.     }
  19.  
  20.     public long getItemId(int position) {
  21.         return tweetList.get(position).getId();
  22.     }
  23.  
  24.     public View getView(int position, View convertView, ViewGroup parent) {
  25.         LinearLayout itemLayout;
  26.         Tweet tweet = tweets.get(position);
  27.  
  28.         itemLayout= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.tweet, parent, false);
  29.  
  30.         TextView tvUser = (TextView) itemLayout.findViewById(R.id.TweetUserName);
  31.         tvUser.setText(tweet.getUsername());
  32.  
  33.         TextView tvText = (TextView) itemLayout.findViewById(R.id.TweetText);
  34.         tvText.setText(tweet.getText());
  35.  
  36.         return itemLayout;
  37.     }
  38.  
  39. }

The method getView now returns a custom design for each list-item based on the tweet.xml. To use this new TweetListAdapter within your ListActivity you should set it to ListActivity's ListView. Therefore the onCreate-method is recommended.
  1. public class TweetListActivity extends ListActivity {
  2.  
  3.     @Override
  4.     public void onCreate(Bundle savedInstanceState) {
  5.         super.onCreate(savedInstanceState);
  6.         List<Tweet> tweetList = loadTweets();
  7.  
  8.         ListAdapter adapter = new TweetListAdapter(tweetList, this);
  9.         getListView().setAdapter(adapter);
  10.     }
  11.  
  12. }

That's all. If you now run your Activity within the SDKs emulator every item should look like defined in tweet.xml. If you like to you can extend the tweet.xml on your own and add things like an image and/or the tweet's posting-time. If you do so the getView-method of TweetListAdapter should be extended too to fill all nested fields.

Comments

 Twitter Trackbacks for Android: Dealing with ListActivities's picture

[...] Android: Dealing with ListActivities, customized ListAdapters and custom-designed items | united-cod... united-coders.com/phillip-steffensen/android-deal...es-customized-listadapters-and-custom-designed-0 – view page – cached Android: Dealing with ListActivities, customized ListAdapters and custom-designed items Tweets about this link [...]

Anonymous's picture

Is it possible to use item list from java cod (not from XML) ?

Nico Heid's picture

of course you can program the layouts in java code without xml. the xml is just a help, so that you can separate layout from code.

this section might interest you: http://developer.android.com/guide/topics/ui/declaring-layout.html

John's picture

Very helpful, thanks!

Anonymous's picture

You didn't define your Tweet class

Guy Fomi's picture

helped me a lot...thx

Henno's picture

Heute war ich am Fummeln mit Listadapter und wessen Blog hilft mir weiter? Deiner.

Hochachtungsvoll ohne Unterton
dein Henrik

Henrik E's picture

t's a very nice and clear example! It helped me a lot - thx again!

Anonymous's picture