我确实调用了一个 AsyncTask 传递一个要执行的方法,以确定一个返回NAME
从特定 id 命名的行的方法。在 doInBackground 上调用数据库查询/原始查询时,它返回一个带有空结果的游标,甚至SELECT * FROM TABLE
在 TABLE 有 8 条记录时尝试使用查询。这里有什么问题?查询是否不同步?如果是,我应该如何编码以防止此类问题?
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)});
调试器总是以空结果显示它