Click here to Skip to main content
15,892,281 members
Articles / Mobile Apps / Android

Custom list item layout

Rate me:
Please Sign up or sign in to vote.
4.85/5 (11 votes)
23 Jan 2012CPOL7 min read 51.9K   1.9K   20  
Describe how to create list of items with custom defined layout resource
package net.origami.android.examples;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CustomAdapterConverterView extends BaseAdapter {
	private final String[] _items;
	private final LayoutInflater _inflater;

	public CustomAdapterConverterView(Context context, String... items) {
		_inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		_items = items;
	}

	@Override
	public int getCount() {
		return _items.length;
	}

	@Override
	public Object getItem(int position) {
		return _items[position];
	}

	@Override
	public long getItemId(int position) {
		return position + 1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 1.
		if (convertView == null) {
			convertView = _inflater.inflate(R.layout.item, null);
		}

		// 2.
		TextView positionText = (TextView) convertView
				.findViewById(R.id.position);
		TextView idText = (TextView) convertView.findViewById(R.id.id);
		TextView itemText = (TextView) convertView.findViewById(R.id.item);

		// 3.
		positionText.setText("position: " + position);
		idText.setText("id: " + getItemId(position));
		itemText.setText("item: " + getItem(position));

		return convertView;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead ASEC S.A. (member of OTI group)
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions