Custom Adapter for ListView

We need to create Adapter for ListView.
Basically, Adapter creates and becomes data store for ListView.
The default is Sting, there are examples of easy instruction for it.

Layout

This is an example layout of ListView.
countrylist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView 
        android:id="@+id/countrylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

This includes only ListView.

Adapter

Custom Adapter should extends ArrayAdapter
Adapter has 2 elements

  • How we create view(fill UI)
  • Data

We can create custom Data class for Adapter.
We should create ListView row in Adapter

Data class

CountryData

public class CountryData 
{
	private int code;
	
	private Bitmap img;
	
	private String name;
	
	private boolean sync;
	
	public CountryData( int code,  Bitmap file, String name, boolean sync ) 
	{
		this.code = code;
		this.img = file;
		this.name = name;
		this.sync = sync;
	}
	
	public int getCode() 
	{
		return code;
	}

	public Bitmap getImg() 
	{
		return img;
	}

	public String getName() 
	{
		return name;
	}

	public boolean isSync() 
	{
		return sync;
	}

public static ArrayList<CountryData> getCountryData( Context context )
	{
		ArrayList<CountryData> list = new ArrayList<CountryData>();
		list.add(new CountryData(
				1,
				BitmapFactory.decodeResource(context.getResources(), R.drawable.united_states),
				"United States",
				false));
		list.add(new CountryData(
				2,
				BitmapFactory.decodeResource(context.getResources(), R.drawable.japan),
				"Japan",
				false));
		list.add(new CountryData(
				3,
				BitmapFactory.decodeResource(context.getResources(), R.drawable.singapore),
				"Singapore",
				false));
		list.add(new CountryData(
				4,
				BitmapFactory.decodeResource(context.getResources(), R.drawable.china),
				"China",
				false));
		list.add(new CountryData(
				5,
				BitmapFactory.decodeResource(context.getResources(), R.drawable.united_kingdom),
				"United Kingdom",
				false));
		return list;
	}
}

Adapter example

CountryAdapter

public class CountryAdapter extends ArrayAdapter<CountryData> 
{
	private SparseBooleanArray checks;
	
	public CountryAdapter(Context context, ArrayList<CountryData> data) 
	{
		super(context, R.layout.country_row, data);
		this.checks = new SparseBooleanArray(5);   // 5 Countries
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		CountryData country = getItem(position);
		
		if ( convertView == null) 
		{
			convertView = LayoutInflater.from(getContext()).inflate(R.layout.country_row, parent, false);
		}
		
		// Fill parameters
		ImageView countryImg = (ImageView)convertView.findViewById(R.id.countryimg);
		countryImg.setImageBitmap(country.getImg());
		TextView nameView = (TextView)convertView.findViewById(R.id.countryname);
		nameView.setText(country.getName());
		CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.check);
		checkBox.setChecked(country.isSync());
		checks.put(country.getCode(), country.isSync());
		checkBox.setOnCheckedChangeListener(new CheckedClass( country.getCode() ));
		return convertView;
	}
	
	public SparseBooleanArray getChecks() 
	{
		return checks;
	}

	class CheckedClass implements OnCheckedChangeListener
	{
		private int code;
		
		public CheckedClass( int code )
		{
			this.code = code;
		}

		@Override
		public void onCheckedChanged(CompoundButton button, boolean checked) 
		{
			// Check code
			checks.put(code, checked);
		}
	}
	
}

How to use that?

This is short codes for it!

View v = getActivity().getLayoutInflater().inflate(R.layout.countrylist, null);
ListView list = (ListView)v.findViewById(R.id.countrylist);
		
CountryAdapter adapter = new CountryAdapter(getActivity(), CountryData.getCountryData(getActivity())); // Create data list
list.setAdapter(adapter);

Get list using findViewById and set adapter

Result

This is example of custom dialog. I use countrylist.xml as dialog layout.
Custom Adapter