private class notesdb extends AsyncTask<Void, Void, Integer> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//displayProgressBar("Downloading...");
}
@Override
protected Integer doInBackground(Void... params) {
db.getCount();
result = db.getCount();
return result;
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
Log.e(result+"",result+"");
if(result==null){
Log.e("db","empty")
}else{
Log.e("db","not empty")
}
//dismissProgressBar();
}
}
int result = 0;
new notesdb().onPostExecute(result);
public int getCount() {
String countQuery = "SELECT * FROM " + TABLE_NOTES;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
return count;
}
我有这个aynctask检查数据库是否为null但是当我运行它总是返回相同的值总是清空这个代码中有什么问题?任何想法
更新:
private class notesdb extends AsyncTask<Void, Void, Integer> {
@Override
protected void onPreExecute() {
//super.onPreExecute();
//displayProgressBar("Downloading...");
}
@Override
protected Integer doInBackground(Void... params) {
db.getCount();
int result = db.getCount();
return result;
}
@Override
protected void onPostExecute(Integer result) {
// super.onPostExecute(result);
Log.e(result+"",result+"");
if(result==null){
Log.e("db","empty");
}else{
Log.e("db","not empty");
}
//dismissProgressBar();
}
}
Button..setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new notesdb().execute();
}
});
答案 0 :(得分:1)
移除对super.onPreExecute();
移除对super.onPostExecute(result);
同时删除这些行:
int result = 0;
new notesdb().onPostExecute(result);
像这样创建一个新的notesdb AsyncTask:
new notesdb().execute();
如果它仍然返回0,请确保填充了数据库表。
答案 1 :(得分:1)
使用
启动异步任务new notesdb().execute();
而不是
new notesdb().onPostExecute(result);