Java ArrayAdapter getView() 方法中的“convertView”参数是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4382176/
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-14 16:45:02  来源:igfitidea点击:

What is "convertView" parameter in ArrayAdapter getView() method

javaandroid

提问by Tarik

Can someone tell me what the convertViewparameter is used for in the getView()method of the Adapterclass?

有人能告诉我这个convertView参数getView()Adapter类的方法中是做什么用的吗?

Here is a sample code take from here:

这是从这里获取的示例代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.row, null);
    }
    Order o = items.get(position);
    if (o != null) {
        TextView tt = (TextView) v.findViewById(R.id.toptext);
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        if (tt != null) {
            tt.setText("Name: "+o.getOrderName());                            }
        if(bt != null){
            bt.setText("Status: "+ o.getOrderStatus());
        }
    }
    return v;
}

What should we pass via convertView?

我们应该通过convertView什么?

What I've found, take from here:

我发现了什么,从这里获取

Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use inflate(int, android.view.ViewGroup, boolean) to specify a root view and to prevent attachment to the root.

Parameters

position-- The position of the item within the adapter's data set of the item whose view we want.

convertView-- The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.

parent-- The parent that this view will eventually be attached to Returns

returns-- A View corresponding to the data at the specified position.

获取在数据集中指定位置显示数据的视图。您可以手动创建视图或从 XML 布局文件对其进行扩充。当视图膨胀时,父视图(GridView、ListView...)将应用默认布局参数,除非您使用 inflate(int, android.view.ViewGroup, boolean) 指定根视图并防止附加到根视图。

参数

position-- 项目在我们想要其视图的项目的适配器数据集中的位置。

convertView-- 如果可能,要重用的旧视图。注意:在使用之前,您应该检查此视图是否为非空且类型合适。如果无法转换此视图以显示正确的数据,则此方法可以创建一个新视图。

parent-- 此视图最终将附加到的父项 Returns

返回-- 与指定位置的数据对应的视图。

采纳答案by Macarse

You shouldn't be calling that method by yourself.

您不应该自己调用该方法。

Android's ListViewuses an Adapterto fill itself with Views. When the ListViewis shown, it starts calling getView()to populate itself. When the user scrolls a new view should be created, so for performance the ListViewsends the Adapteran old view that it's not used any more in the convertViewparam.

AndroidListView使用 anAdapter来填充自己Views。当ListView显示 时,它开始调用getView()以填充自身。当用户滚动时,应该创建一个新视图,因此为了提高性能,它会ListView发送Adapter一个旧视图,它在convertView参数中不再使用。