你能告诉我如何找到这个方法的正确参数:getView(int position, View convertView, ViewGroup parent)
我有一个带有自定义适配器的 ListView。列表视图如下所示:
TextView EditText
TextView EditText
TextView EditText...
convertView 和 parent 参数是什么?
你能告诉我如何找到这个方法的正确参数:getView(int position, View convertView, ViewGroup parent)
我有一个带有自定义适配器的 ListView。列表视图如下所示:
TextView EditText
TextView EditText
TextView EditText...
convertView 和 parent 参数是什么?
getView
当要显示列表项时,将调用自定义适配器的方法。该方法不需要提供参数,Android系统会提供。“parent”将是您的列表视图,“convertView”将在显示第一个列表项时为空。您还可以重复使用 convertView。实现自定义适配器的正确方法是http://developer.samsung.com/android/technical-docs/Android-UI-Tips-and-Tricks
找什么是什么意思?
如果您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();
}
});
所以:
我希望这有帮助
getView()
是一种运行多次的方法,每次您的程序在列表中填充一行时,它都会运行。父级是您的自定义适配器,您可以在其中添加一行。
是适配器convertView
中相应位置的行的 GUI(视图) 。Position