-1

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

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

TextView EditText
TextView EditText
TextView EditText...

convertView 和 parent 参数是什么?

4

4 回答 4

1

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

于 2013-06-21T13:44:28.353 回答
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();

      }                 
});
于 2013-06-21T13:54:58.137 回答
1

所以:

  1. position 是视图显示的数据在数据集中的位置。
  2. convertView 用于回收视图,这样您就不会一次运行一堆
  3. parent 是视图的包含适配器,在这种情况下,它是我假设的 ListView。

我希望这有帮助

于 2013-06-21T13:50:13.343 回答
0

getView()是一种运行多次的方法,每次您的程序在列表中填充一行时,它都会运行。父级是您的自定义适配器,您可以在其中添加一行。 是适配器convertView中相应位置的行的 GUI(视图) 。Position

于 2015-08-24T09:52:18.767 回答