can any one provide me example of Two_line_list_item in Android? [closed]

Can any one provide me two_line_list_item example?

Asked by: Carlos822 | Posted: 24-01-2022

Answer 1

I have yet to find an example that actually uses the built-in layout, android.R.layout.two_line_list_item and a ListView insted of ListActivity. So here goes.

If you're in a hurry, the TwoLineArrayAdapter.getView() override below is the important part of using the default two_line_list_item layout.

Your data

You have a class that defines your list items. I'll assume you have an array of these.

public class Employee {
    public String name;
    public String title;

An abstract TwoLineArrayAdapter

This abstract class can be reused, and makes defining a two-line ListView much easier later. You can supply your own layout, but the two argument constructor uses the built-in two_line_list_item layout. The only requirement for custom list item layouts is that they must use @android:id/text1 and @android:id/text2 to identify their TextView children, just as two_line_list_item does.

public abstract class TwoLineArrayAdapter<T> extends ArrayAdapter<T> {
        private int mListItemLayoutResId;

        public TwoLineArrayAdapter(Context context, T[] ts) {
            this(context, android.R.layout.two_line_list_item, ts);

        public TwoLineArrayAdapter(
                Context context, 
                int listItemLayoutResourceId,
                T[] ts) {
            super(context, listItemLayoutResourceId, ts);
            mListItemLayoutResId = listItemLayoutResourceId;

        public android.view.View getView(
                int position, 
                View convertView,
                ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater)getContext()

            View listItemView = convertView;
            if (null == convertView) { 
                listItemView = inflater.inflate(

            // The ListItemLayout must use the standard text item IDs.
            TextView lineOneView = (TextView)listItemView.findViewById(
            TextView lineTwoView = (TextView)listItemView.findViewById(

            T t = (T)getItem(position); 

            return listItemView;

        public abstract String lineOneText(T t);

        public abstract String lineTwoText(T t);

A concrete TwoLineArrayAdapter

Finally, here's the code you write specific to your Employee class so that it'll render in your ListView.

public class EmployeeArrayAdapter extends TwoLineArrayAdapter<Employee> {
    public EmployeeArrayAdapter(Context context, Employee[] employees) {
        super(context, employees);

    public String lineOneText(Employee e) {

    public String lineTwoText(Employee e) {
        return e.title;


In your Activity's onCreate() method, you'll have code that looks like this:

    employees = new Employee[...];
    //...populate the employee array...

    employeeLV = (ListView)findViewById(;
    employeeLV.setAdapter(new EmployeeArrayAdapter(this, employees);

Answered by: Julian704 | Posted: 25-02-2022

Similar questions

java - How to use the two_line_list_item in android?

I have a ListView and the following adapter: ArrayAdapter&lt;Item&gt; adapter = new ArrayAdapter&lt;Item&gt;(this, android.R.layout.two_line_list_item, items); where items has the following type: ArrayList&lt;Item&gt; The class items looks like: public class Item { String foo1; String foo2; public String getLine1(...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android