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

i have this aynctask that check database if null or not but when i run it always return the same value always empty what is wrong in this 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();
            }
        });
Giant
  • 1,619
  • 7
  • 33
  • 67

2 Answers2

1

Remove the call to super.onPreExecute();

Remove the call to super.onPostExecute(result);

Also remove these lines:

int result = 0;
new notesdb().onPostExecute(result);

Create a new notesdb AsyncTask like this:

new notesdb().execute();

If it's still returning 0, make sure that your database table is populated.

JDJ
  • 4,298
  • 3
  • 25
  • 44
  • now i get the same error as before i get this processWorkerExit(w, completedAbruptly); error when i use .execute – Giant Jun 27 '14 at 07:54
  • Where are you making this call: `new notesdb().execute()` ? In `onCreate()`? – JDJ Jun 27 '14 at 08:01
  • Are you certain that the database table is populated? – JDJ Jun 27 '14 at 08:14
  • you are correct sir because of the processWorkerExit(w, completedAbruptly); i wasnt aware that i get null pointer on database thanks to Lazy Ninja i was able to determine that i get a null pointer you get the +1 sir – Giant Jun 27 '14 at 08:38
  • one more question in this same activity can i put another asynctask for different database? – Giant Jun 27 '14 at 08:39
  • 1
    Yes, you can have multiple AsyncTasks. – JDJ Jun 27 '14 at 08:43
1

Start your async task with

new notesdb().execute();

instead of

new notesdb().onPostExecute(result);
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103