Android ListView Custom Adapter with ImageView

This post describes how to use Android Listview Custom adapter with Imageview to customise the listview row layout and interaction. 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.

android listview custom adapter
We want that each row in the

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:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
android:layout_height="wrap_content" >


android:textStyle="bold" />

android:textStyle="italic" />


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:

private static class PlanetHolder {
public TextView planetNameView;
public TextView distView;
public ImageView img;

and the adapter becomes:

public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;

PlanetHolder holder = new PlanetHolder();

// First let's verify the convertView is not null
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.img_row_layout, null);
// Now we can fill the layout with the right values
TextView tv = (TextView) v.findViewById(;
TextView distView = (TextView) v.findViewById(;
ImageView img = (ImageView) v.findViewById(;

holder.planetNameView = tv;
holder.distView = distView;
holder.img = img;

holder = (PlanetHolder) v.getTag();

System.out.println("Position ["+position+"]");
Planet p = planetList.get(position);
holder.distView.setText("" + p.getDistance());


return v;

In the Android list view custom adapter using 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:
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:

Planet planet =  aAdpt.getItem(pos);

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


A new version of Android, called Android Lollipop, has introduced  a new widget called RecyclerView. If you want to know more read this post.

More posts...
  • Nice post.Give it up. Thanks for share this article. For more visit:android development

  • Gref

    This doesn't work (updating the sourcecode from with these new codes). How would it know which image to put into each row? You just set an integer ID, but how does it connect this id to the right resource? It doesn't, and it doesn't load the images when you go with the above code. Could you please provide the full source code or correct the above? Thanks a lot for your useful tutorials they are very appreciated!