Tutorial details

Android ListView: Custom Adapter with ImageView | App Code for Sale | Preview

Android ListView: Custom Adapter with ImageView | Android Tutorial

How to modify the custom adapter in order to handle the images and the holder class

Overview PAGE TOP

In previous post we talked about custom adapter. We used a quite simple adapter with just a TextView. We want to expand the idea described before and introduce an image for each planet. To do it we have to modify the layout and other code parts to handle the user click on each item. We want that each row in the ListView looks like:


The first thing we have to do is to modify the layout and use a RelativeLayout instead of a simple LinearLayout. Our layout looks like:

01  <?xml version="1.0" encoding="utf-8"?>
02  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03      android:layout_width="match_parent"
04      android:layout_height="wrap_content" >
06      <ImageView
07          android:id="@+id/img"
08          android:layout_width="wrap_content"
09          android:layout_height="match_parent"
10          android:layout_alignParentLeft="true"/>
12      <TextView
13          android:id="@+id/name"
14          android:layout_width="wrap_content"
15          android:layout_height="wrap_content"
16          android:layout_alignTop="@+id/img"
17          android:layout_toRightOf="@+id/img"
18          android:textStyle="bold" />
20      <TextView
21          android:id="@+id/dist"
22          android:layout_width="wrap_content"
23          android:layout_height="wrap_content"
24          android:layout_below="@id/name"
25          android:layout_gravity="center"
26          android:layout_marginTop="2dip"
27          android:layout_toRightOf="@id/img"
28          android:gravity="right"
29          android:textSize="8dp"
30          android:textStyle="italic" />
32  </RelativeLayout>

The layout shown above is quite trivial and we don’t need to explain it. Then we have to modify the custom adapter in order to handle the images and the holder class. The holder class becomes:

1   private static class PlanetHolder {
2       public TextView planetNameView;
3       public TextView distView;
4       public ImageView img;
5   }

and the adapter becomes:

01  public View getView(int position, View convertView, ViewGroup parent) {
02      View v = convertView;
04      PlanetHolder holder = new PlanetHolder();
06      // First let's verify the convertView is not null
07      if (convertView == null) {
08          // This a new view we inflate the new layout
09          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
10          v = inflater.inflate(R.layout.img_row_layout, null);
11          // Now we can fill the layout with the right values
12          TextView tv = (TextView) v.findViewById(R.id.name);
13          TextView distView = (TextView) v.findViewById(R.id.dist);
14          ImageView img = (ImageView) v.findViewById(R.id.img);
16          holder.planetNameView = tv;
17          holder.distView = distView;
18          holder.img = img;
20          v.setTag(holder);
21      }
22      else
23          holder = (PlanetHolder) v.getTag();
25      System.out.println("Position ["+position+"]");
26      Planet p = planetList.get(position);
27      holder.planetNameView.setText(p.getName());
28      holder.distView.setText("" + p.getDistance());
30      holder.img.setImageResource(p.getIdImg());
32      return v;
33  }

In this way for each row in the ListView we set not only the planet name in the TextView but the ImageView also, with the image relative to the planet. If we run the app we have something like it:


Well i didn’t use the right images for the planets it is just a way to show you how to use images!!!. You have to resize and make the images better in your app!

Now if you click on an item you get a ClassCastException. Why? Well, you try to cast, inside the onItemClick method, a RelativeLayout to a TextView and it is impossible.

Handle user item click

If you want to handle user click on each item you have to modify the code inside the method lv.setOnItemClickListener. First of all we have to find the item position chosen by the user and we could do it or using the position parameter in public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) or we could ask to the SO to find the right position in this way:

1   int pos = lv.getPositionForView(view);

In this way we know the position of the item clicked by the user. Once we know it, it is really simple to extract the information, because having the reference to the array of items we simply do:

1   Planet planet =  aAdpt.getItem(pos);

So when user clicks on an item inside the ListView we have:


Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.