查看 github 上的 CursorAdapter 代码(core/java/android/widget/CursorAdapter.java)
getView 方法调用 mCursor.moveToPosition
public View getView(int position, View convertView, ViewGroup parent) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
View v;
if (convertView == null) {
v = newView(mContext, mCursor, parent);
} else {
v = convertView;
}
bindView(v, mContext, mCursor);
return v;
}
当我在这里查看 CursorAdapter 代码时,会有一些情况下 fillWindow 被调用core/java/android/database/sqlite/SQLiteCursor.java
@Override
public boolean onMove(int oldPosition, int newPosition) {
// Make sure the row at newPosition is present in the window
if (mWindow == null || newPosition < mWindow.getStartPosition() ||
newPosition >= (mWindow.getStartPosition() + mWindow.getNumRows())) {
fillWindow(newPosition);
}
return true;
}
如果您继续查看代码,您会发现这最终会进行数据库调用。在 UI 线程上进行数据库调用的原因是什么?