开发者

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

开发者 https://www.devze.com 2022-12-17 10:58 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books开发者_运维技巧, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 5 years ago.

Improve this question

Can any one provide me two_line_list_item example?


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

精彩评论

暂无评论...
验证码 换一张
取 消