April 29, 2011

How to create a listview with custom adapter on Android

To create a ListView with your custom layout you need to create a custom adapter, override the getView method, and set the list adapter.
Find below an example with a TextView, ImageView and Button in the layout.
















main.xml

    
    
    
    
    




main.java
package com.blog.listviewwithcustomadapter;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class main extends ListActivity {

 public final String[] Colors = { "Blue", "Green", "Black" };

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // setContentView(R.layout.main);
  // instantiate our custom adapter
  MyCustomAdapter adapter = new MyCustomAdapter(this, R.layout.main, Colors);
  //setting the adapter
  setListAdapter(adapter);
 }

 public class MyCustomAdapter extends ArrayAdapter {

  public MyCustomAdapter(Context context, int textViewResourceId, String[] list) {
   super(context, textViewResourceId, list);
  }

  @Override
     public View getView(int position, View convertView, ViewGroup parent) {
            
      View row = convertView;

      if (row == null) {
       LayoutInflater inflater = getLayoutInflater();
       row = inflater.inflate(R.layout.main, parent, false);
      }
      final int itemPosition = position;
      
      TextView listItem = (TextView) row.findViewById(R.id.colors );
      listItem.setText(Colors[position]);
      
      ImageView image = (ImageView) row.findViewById(R.id.imgAndroid);
      image.setImageResource(R.drawable.icon);
      
      Button btn = (Button) row.findViewById(R.id.btnColor);
      btn.setOnClickListener( new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
     Toast.makeText(getApplicationContext(), "Toast from button"+ Colors[itemPosition], Toast.LENGTH_SHORT).show();     
    }
   });
            
      row.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
        Toast.makeText(getApplicationContext(), Colors[itemPosition], Toast.LENGTH_SHORT).show();
       }     
      });
      return row;
     }
 }
}

7 comentarii:

Manjeet Singh Ranawat said...

hey dude thanks alot a very good example of list having image button and textview.

Mobi People said...

Android applications development are very famous for in the mobile era.

You can get absolutely customized services with the help of the developers and programmers as per your business requirements which proves the potentiality of the people that are hired.

Anonymous said...

i'm havin a pb in thz this

<<<<<< {

public MyCustomAdapter(Context context, int textViewResourceId, String[] list) {
super(context, textViewResourceId, list);
}
>>>>>>>>>>>>

please can u snd me if it iz possible 2 ratnabasha@gmail.com tnx it'll be really usefull

Anonymous said...

thanks, is is help me

Anonymous said...

yup, problemn over


public MyCustomAdapter(Context context, int textViewResourceId, String[] list) {
super(context, textViewResourceId, list);
}

nice blog, keep the good work going

Anonymous said...

sry, meant to say same problemn not that it's over.

Anonymous said...

Appreciate this alot!!

Post a Comment