Thursday, 11 August 2016

Custom Listview with Imageview and Textview in Android

Android provides Listview to display items in list. But there is drawback on Android basic Listview, you can not a customize it. So to make own Custom Listview you need to implement own code.

In this tutorial we are going to implement Custom Listview using BaseAdapter class.BaseAdapter class provides a set of methods which is used to create the custom list.


First create activity_main.xml




 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textStyle="bold"
        android:text=" Countries " />

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout> 

Now create a layout for list item which is displayed in Listview as a row. Create new file in layout folder named a list_row.xml



 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


 <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textStyle="bold"  />

</LinearLayout> 


Now create Model named as CountryModel.java. This is used to send data between Activity and Adapter class.





public class CountryModel {

private int image;
private String countryName;

public void setImage(int a)
{
image = a;
} 

public void setCountry(String a)
{
countryName = a;
} 
public int getImage()
{
return  image;
} 
public String getCountry()
{
return  countryName;
} 
} 


Now create Adapter class named as CountryAdapter.java.




public class CountryAdapter extends BaseAdapter{

ArrayList<CountryModel>  list;
LayoutInflater inflater;
ViewHolder holder;
public CountryAdapter(Context c,ArrayList<CountryModel> list){
this.list=list;
inflater=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int arg0) {
return list.get(arg0);
}

@Override
public long getItemId(int arg0) {
return 0; 
}

@Override
public View getView(int position,View convertView,ViewGroup arg2) {

View vi = convertView;

if (convertView==null){
           
         vi = inflater.inflate(R.layout.list_row, null);
         
         holder = new ViewHolder();
         holder.image = (ImageView) vi.findViewById(R.id.img);
         holder.countryname=(TextView)vi.findViewById(R.id.tv);
         
         vi.setTag( holder );
     }
     else 
         holder=(ViewHolder)vi.getTag();
     CountryModel m = list.get(position);
     holder.image.setImageResource(m.getImage());
     holder.countryname.setText(m.getCountry());
 return vi;
}

public static class ViewHolder{
   
      public ImageView image;
      public TextView countryname;
   }
}


Now create MainActivity.java file and assign custom list to listview.





public class MainActivity  extends Activity {

CountryModel model;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList list = new ArrayList();
        model=new CountryModel();
        model.setImage(R.drawable.flag_canada);
        model.setCountry("Canada");
        list.add(model);
        model=new CountryModel();
        model.setImage(R.drawable.flag_france);
        model.setCountry("France");
        list.add(model);
        model=new CountryModel();
        model.setImage(R.drawable.flag_india);
        model.setCountry("India");
        list.add(model);
        model=new CountryModel();
        model.setImage(R.drawable.flag_poland);
        model.setCountry("Poland");
        list.add(model);
        model=new CountryModel();
        model.setImage(R.drawable.flag_russia);
        model.setCountry("Russia");
        list.add(model);
        model=new CountryModel();
        model.setImage(R.drawable.flag_uk);
        model.setCountry("UK");
        list.add(model);
        model=new CountryModel();
        model.setImage(R.drawable.flag_usa);
        model.setCountry("Usa");
        list.add(model);
        
        ListView lv =(ListView)findViewById(R.id.listview);
        CountryAdapter adapter =new CountryAdapter(this, list);
        lv.setAdapter(adapter);
        
  }
}




No comments:

Post a Comment