首页 文章

getView方法的参数 - android

提问于
浏览
-1

你能告诉我如何找到这个方法的正确参数:getView(int position,View convertView,ViewGroup parent)

我有一个带有自定义适配器的ListView . 列表视图如下所示:

TextView EditText
TextView EditText
TextView EditText ...

什么是convertView和parent参数?

4 回答

  • 1

    所以:

    • position是数据集中视图显示的数据的位置 .

    • convertView用于回收视图,因此您不会立即运行一堆

    • parent是视图的包含适配器,对于这种情况,它将是我假设的ListView .

    我希望这有帮助

  • 0

    当要显示列表项时,将调用自定义适配器的 getView 方法 . 您不需要为方法提供参数,Android系统会提供它们 . "parent"将是您的列表视图,"convertView"将在显示第一个列表项时为空 . 您还可以重用convertView . 实现自定义适配器的正确方法是http://developer.samsung.com/android/technical-docs/Android-UI-Tips-and-Tricks

  • 1

    你找到什么意思?

    如果您在适配器类中使用了 override getView() 方法,那么您将能够知道每一行的视图内容 .

    这将是这样的:

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
      TextView textView = (TextView) convertView.findViewById(R.id.textView_id);
      EditText editText = (EditText) convertView.findViewById(R.id.editText_id);
      // position param is the the correspondent line on the list to this view, you can use this parameter to do anything like:
    
      if(position==0) {
          textView.setText("This is the first line!");
      }
    
     // Do anything you want with your views... populate them. This is the place where will be defined the content of each view.
    }
    

    如果您已经填充了列表并希望在选择视图时检索某些值,则实现 ListView 的监听器,如下所示 .

    final ListView list = (ListView) findViewById(R.id.listView_id);
    
    list.setOnItemClickListener(new OnItemClickListener() {
    
          public void onItemClick(AdapterView<?> adapter, View view, int position, long long) {
              TextView textView = (TextView) view.findViewById(R.id.textView_id); 
              EditText editText = (EditText) view.findViewById(R.id.editText_id);
    
              String textViewText = textView.getText();
              String editTextText = editText.getText().toString();
    
          }                 
    });
    
  • 1

    getView() 是一种运行多次的方法,每当您的程序在列表中膨胀它将运行时 . 父级是您向其中添加行的自定义适配器 . convertView 是适配器中 Position 位置的行的GUI(视图) .

相关问题