android中CursorAdapter的bindview调用
由于自己写了一个CustomAdapter来继承CursorAdapter,要重写bindView和newView方法。
当一个ListActivity的启动时,onCreate方法中会setListAdapter为CustomAdapter。
当我打Log的时候发现每一个Item中bindView方法被调用了两次。也就是说,如果你有3个Item在List中,bindView就被调用了6次。
我想问一下CursorAdapter的bindView方法在什么时候被调用,为什么一个Item要调用两次bindView来显示?谢谢大家
这样的情形导致的问题是,有时候在查看List的时候,会发现字体出现明显的抖动,因为第一次刷新显示了所有了Item,但是后面又刷新显示了一次,这是因为每个Item两次调用bindView所致。当然,这种问题只有在数据量比较大的时候会出现延迟显示。不知道哪位高人能不能帮我解决一下。下面是简化的代码:
public class MyNotepad extends ListActivity {
private NotesDbAdapter mDbHelper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes_list);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
CustomAdapter adapter = new CustomAdapter (this, notesCursor);
setListAdapter(adapter);
}
final class CustomAdapter extends CursorAdapter{
public CustomAdapter (Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.i("huaping", "newView");
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.notes_row, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
Log.i("huaping", "bindView");
String title = cursor.getString(KEY_TITLE);
final TextView views = (TextView) view.findViewById(R.id.text1);
views.setText(title);
}
}
}