【发布时间】:2010-06-25 20:16:12
【问题描述】:
我在使用从书中改编的一些 BaseAdapter 代码时遇到问题。我一直在我的应用程序中到处使用此代码的变体,但只是在滚动长列表时才意识到 ListView 中的项目变得混乱,并且并非所有元素都显示出来。
很难描述确切的行为,但很容易看出您是否将 50 个项目的排序列表开始向上和向下滚动。
class ContactAdapter extends BaseAdapter {
ArrayList<Contact> mContacts;
public ContactAdapter(ArrayList<Contact> contacts) {
mContacts = contacts;
}
@Override
public int getCount() {
return mContacts.size();
}
@Override
public Object getItem(int position) {
return mContacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null){
LayoutInflater li = getLayoutInflater();
view = li.inflate(R.layout.groups_item, null);
TextView label = (TextView)view.findViewById(R.id.groups_item_title);
label.setText(mContacts.get(position).getName());
label = (TextView)view.findViewById(R.id.groups_item_subtitle);
label.setText(mContacts.get(position).getNumber());
}
else
{
view = convertView;
}
return view;
}
}
【问题讨论】: