Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么CursorAdapter(使用ContentProvider)在UI线程上加载数据?_Android_Android Cursoradapter_Performance - Fatal编程技术网

Android 为什么CursorAdapter(使用ContentProvider)在UI线程上加载数据?

Android 为什么CursorAdapter(使用ContentProvider)在UI线程上加载数据?,android,android-cursoradapter,performance,Android,Android Cursoradapter,Performance,查看github()上的CursorAdapter代码 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"

查看github()上的CursorAdapter代码

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的情况

@覆盖
公共布尔onMove(int-oldPosition,int-newPosition){
//确保窗口中存在newPosition处的行
if(mWindow==null | | newPosition=(mWindow.getStartPosition()+mWindow.getNumRows()){
填充窗口(新位置);
}
返回true;
}

如果您继续阅读代码,您将看到它最终将进行数据库调用。在UI线程上执行数据库调用的原因是什么?

据我所知,光标的窗口部分是为了对用户隐藏的 调用程序,因此在使用当前API从第一个窗口转到溢出窗口时,无法避免从主线程命中DB

不过,大多数查询都可以放在一个窗口中,因此通常的做法是在AsynTask或Loader中的游标上调用getCount(),然后再将其交还给UI线程,这会产生加载第一个窗口的副作用

@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;
}