android - AsyncTask<Void, Void, Integer> always return 0 -
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 * " + table_notes; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); int count = cursor.getcount(); cursor.close(); return count; }
i have aynctask check database if null or not when run return same value empty wrong in code?any idea
update:
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(); } });
remove call super.onpreexecute();
remove call super.onpostexecute(result);
also remove these lines:
int result = 0; new notesdb().onpostexecute(result);
create new notesdb asynctask this:
new notesdb().execute();
if it's still returning 0, make sure database table populated.
Comments
Post a Comment