Skip to content Skip to sidebar Skip to footer

Can Any One Provide Me Example Of Two_line_list_item In Android?

Can any one provide me two_line_list_item example?

Solution 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.

publicclassEmployee{
    publicString name;
    publicString 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.

publicabstractclassTwoLineArrayAdapter<T> extendsArrayAdapter<T> {
        privateint mListItemLayoutResId;

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

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

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


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

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

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

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

            return listItemView;
        }

        publicabstract String lineOneText(T t);

        publicabstract 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.

publicclassEmployeeArrayAdapterextendsTwoLineArrayAdapter<Employee> {
    publicEmployeeArrayAdapter(Context context, Employee[] employees) {
        super(context, employees);
    }

    @OverridepublicStringlineOneText(Employee e) {
        return e.name;
    }

    @OverridepublicStringlineTwoText(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(newEmployeeArrayAdapter(this, employees);

Post a Comment for "Can Any One Provide Me Example Of Two_line_list_item In Android?"