can any one provide me example of Two_line_list_item in Android?

UMAR-MOBITSOLUTIONS picture UMAR-MOBITSOLUTIONS · Jan 21, 2010 · Viewed 24.5k times · Source

Can any one provide me two_line_list_item example?

Answer

ESV picture ESV · Mar 21, 2012

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;
        }

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


            LayoutInflater inflater = (LayoutInflater)getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View listItemView = convertView;
            if (null == convertView) { 
                listItemView = inflater.inflate(
                    mListItemLayoutResId, 
                    parent, 
                    false);
            }

            // The ListItemLayout must use the standard text item IDs.
            TextView lineOneView = (TextView)listItemView.findViewById(
                android.R.id.text1);
            TextView lineTwoView = (TextView)listItemView.findViewById(
                android.R.id.text2);

            T t = (T)getItem(position); 
            lineOneView.setText(lineOneText(t));
            lineTwoView.setText(lineTwoText(t));

            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);
    }

    @Override
    public String lineOneText(Employee e) {
        return e.name;
    }

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

Activity.onCreate()

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

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

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