android sqlite query返回0结果的游标

时间:2013-02-19 23:08:10

标签: android android-asynctask android-sqlite

我确实调用AsyncTask传递要执行的方法,以确定从特定id返回名为NAME的行的方法。在doInBackground上调用数据库查询/原始查询时,它返回一个带有null结果的游标,当TABLE有8条记录时,甚至尝试使用查询SELECT * FROM TABLE。这有什么问题?查询是否不同步?如果是的话,我应该如何编码以防止出现这类问题?

EDIT

...
final TextView tv = (TextView)findViewById(R.id.textView);

dao.retrieveMovieByIdAsync(1, new AsyncTaskCaller() {
    @Override
    public void execute(Movie result) {
        tv.setText(result.get_Name());
    }});
...

private class AsyncRetieveQuery extends AsyncTask<Method, Void, Movie>
{
    private Object args;
    private AsyncTaskCaller signal;

    public AsyncRetieveQuery(Object args, AsyncTaskCaller signal) {
        this.args = args;
        this.signal = signal;
    }

    @Override
    protected Movie doInBackground(Method... params) {
        params[0].setAccessible(true);
        try {
            return (Movie) params[0].invoke(DBUtils.this, new Object[] {args});
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(Movie result) {
        signal.execute(result);
        super.onPostExecute(result);
    }
}
...
public void retrieveMovieByIdAsync(int id, AsyncTaskCaller resultMethod)
{
    try {
        new AsyncRetieveQuery(id,resultMethod).execute(DBUtils.class.getDeclaredMethod("retrieveMovieById", int.class));
    } catch (NoSuchMethodException e) {}
}
...
private Movie retrieveMovieById(int id)
{
    Cursor cursor = database.rawQuery("SELECT * FROM " + SQLiteHelper.TABLE_MOVIES_NAME + " WHERE _Id = ?", new String[] {String.valueOf(id)});

调试器始终使用null结果显示它

1 个答案:

答案 0 :(得分:0)

实际问题是,即使我只需要从答案中获取行数,我也必须调用cursor.moveToFirst()
我认为当谷歌说查询调用后的光标得到最后的结果时,它们可能意味着你在迭代行时用isAfterLast方法检查的空行。