我想详细了解适配器的getView方法中位置参数的意义。在ListView中我想用Textview和按钮设置自定义适配器。
代码如下所述。
public View getView(final int position, View convertView, ViewGroup parent)
{
try
{
if(convertView==null)
{
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.contactslist, null);
name = (TextView) convertView.findViewById(R.id.contactname);
number = (TextView) convertView.findViewById(R.id.contactmobnum);
call=(Button)convertView.findViewById(R.id.callcust);
}
name.setText(cursor.getString(1));
number.setText(cursor.getString(2));
cursor.moveToNext();
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Log.i("Testing","Position selected is "+position);
}
});
} catch(Exception e) {
Log.d("Testing", "isDefault::: exec"+e);
}
return convertView;
}
如果假设我的光标总共有 10 个项目并且我点击了第 5 个位置调用按钮,那么预期的结果是什么?
会是10个还是5个?
很困惑,因为我之前使用过 Base Adapter 并且体验过它为 10,但在上面的代码中它返回 5,请让我了解该位置是否仅在将行添加到列表中时返回行 ID,或者它还返回您选择的行 ID ?
期待更好的理解