任何人都可以为我提供 Android 中 Two_line_list_item 的示例吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2109271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:34:23  来源:igfitidea点击:

can any one provide me example of Two_line_list_item in Android?

androidlistactivity

提问by UMAR

Can any one provide me two_line_list_item example?

任何人都可以为我提供 two_line_list_item 示例吗?

回答by ESV

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

我还没有找到实际使用的例子内置的布局,android.R.layout.two_line_list_itemListViewinsted的的ListActivity。所以就到这里了。

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

如果您赶时间,TwoLineArrayAdapter.getView()下面的覆盖是使用默认two_line_list_item布局的重要部分。

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

一个抽象的 TwoLineArrayAdapter

This abstract class can be reused, and makes defining a two-line ListViewmuch easier later. You cansupply your own layout, but the two argument constructor uses the built-in two_line_list_itemlayout. The only requirement for custom list item layouts is that they must use @android:id/text1and @android:id/text2to identify their TextViewchildren, just as two_line_list_itemdoes.

这个抽象类可以重用,并且使以后定义两行ListView更容易。您可以提供自己的布局,但两个参数构造函数使用内置two_line_list_item布局。 自定义列表项布局的唯一要求是它们必须使用@android:id/text1@android:id/text2标识其TextView子项,就像这样two_line_list_item做一样。

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

一个具体的 TwoLineArrayAdapter

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

最后,这是您为 Employee 类编写的特定代码,以便它将在您的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()

Activity.onCreate()

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

在您的 ActivityonCreate()方法中,您将拥有如下所示的代码:

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

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