我的代码就像下面的例子。它应该只调用一次 newView(),但我不知道为什么它被一次又一次地调用?
public class TimeListAdapter extends CursorAdapter {
private static class ViewHolder {
int nameIndex;
int timeIndex;
TextView name;
TextView time;
}
public TimeListAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.name.setText(cursor.getString(holder.nameIndex));
holder.time.setText(cursor.getString(holder.timeIndex));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup
p parent) {
View view = LayoutInflater.from(context).inflate
p (R.layout.time_row, null);
ViewHolder holder = new ViewHolder();
holder.name = (TextView) view.findViewById(R.id.task_name);
holder.time = (TextView) view.findViewById(R.id.task_time);
holder.nameIndex = cursor.getColumnIndexOrThrow
p (TaskProvider.Task.NAME);
holder.timeIndex = cursor.getColumnIndexOrThrow
p (TaskProvider.Task.DATE);
view.setTag(holder);
return view;
}
}
例如,我有12 个项目要显示,newView() 方法应该只调用一次以重用视图。(如果我错了,请纠正我)。
但在这里它至少被调用了 7 次。我做错什么了吗?else 使用 CursorAdapter 重用 ListView 有什么根本错误吗?