你能告诉我如何为这个方法找到合适的参数吗getView(int position,View convertView,ViewGroup parent)
我有一个带有自定义适配器的ListView。列表视图如下所示:
文本查看编辑文本
文本查看编辑文本
文本查看编辑文本。。。
convertView和父参数是什么?
所以:
- position是视图显示的数据在数据集中的位置
- convertView用于回收视图,这样就不会同时运行一堆视图
- parent是视图的包含适配器,在本例中,我假设它是ListView
我希望这能帮助
getView
方法。您不需要为该方法提供参数,Android系统会提供这些参数。"parent"将是您的列表视图,当显示第一个列表项时,"convertView"将为null。您也可以重用convertView。实现自定义适配器的正确方法是http://developer.samsung.com/android/technical-docs/Android-UI-Tips-and-Tricks
find是什么意思?
如果您在适配器类中使用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
是适配器中Position
位置的行的GUI(视图)。