在getView)的问题( [英] question on getView()

查看:143
本文介绍了在getView)的问题(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可能能解释下code做什么?

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){   // TODO自动生成方法存根
   查看MyView的= NULL;
   尝试{
    MyView的= convertView;
    如果(空== MyView的){
     LayoutInflater李=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);     MyView的= li.inflate(R.layout.demographic_list_item,NULL);    }
    如果(mScan_listItems [位置]!= NULL){
     //读取值并附加它们。
     TextView的TV1 =(TextView中)MyView的
       .findViewById(R.id.DemoGraphicItem_Text);     tv1.setText(mScan_listItems [位置]);    }   }赶上(例外五){
    e.printStackTrace();
   }   返回MyView的;
  } }


解决方案

Adapter.getView 文档给使用getView的一些迹象:

 公共抽象的视图getView(INT位置,查看convertView,父母的ViewGroup)

送,在数据集中的指定位置显示的数据的视图。要么你可以手动创建一个视图或从XML布局文件它充气。当View充气时,父视图(GridView控件,ListView中...),除非你用膨胀将应用默认的布局参数(INT,android.view.ViewGroup,布尔值)指定一个根视图和prevent附件根。

位置

项的适配器的数据集,我们希望其图。该项目中的位置

convertView

老以便如果可能的话再利用。注意:您应该检查这个观点是在使用之前非空和适当的类型。如果它是不可能的该视图以显示正确的数据转换,该方法可以创建新的视图。

母公司,这种观点最终会被附加到

返回

对应的数据的视图在指定的位置。


getView将调用在数据集中的每个项目。从适配器API文档


  

这是适配器对象充当桥梁
  一个AdapterView和之间
  该视图的基础数据。该
  适配器提供对数据的访问
  项目。该适配器还负责
  制作在每个项目视图
  数据集。


Can anyone possible explain what the following code does?

  public View getView(int position, View convertView, ViewGroup parent) {

   // TODO Auto-generated method stub
   View myView = null;
   try {
    myView = convertView;
    if (null == myView) {
     LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     myView = li.inflate(R.layout.demographic_list_item, null);

    }
    if (mScan_listItems[position] != null) {
     // read the values and attach them.
     TextView tv1 = (TextView) myView
       .findViewById(R.id.DemoGraphicItem_Text);

     tv1.setText(mScan_listItems[position]);

    }

   } catch (Exception e) {
    e.printStackTrace();
   }

   return myView;
  }

 }

解决方案

The Adapter.getView docs give some indication of the use of getView:

public abstract View getView (int position, View convertView, ViewGroup parent)

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.

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

A View corresponding to the data at the specified position.


getView will get called for every item in your dataset. From the Adapter API docs:

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

这篇关于在getView)的问题(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆