2016-05-18 3 views
0

Ich mache ein Android-Projekt, wenn ich versuche, cursor.getCount() zu verwenden; es gibt mir eine Fehlermeldung:cursor.getCount() geben Fehler

cannot resolve symbol getCount();

Hier ist mein Code

class GetNotesFromDbTask extends AsyncTask<Void, Void, Boolean> { 

@Override 
protected Boolean doInBackground(Void... params) { 
    return null; 
} 

@Override 
protected void onPreExecute() { 
    ProgressDialog dialog = new ProgressDialog(NoteDetail.this); 
    dialog.setTitle("Loading"); 
    dialog.setMessage("Please wait.."); 
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    dialog.setCancelable(false); 
    dialog.show(); 
} 


SQLiteDatabase db = helpers.getReadableDatabase(); 
String[] projection = { 
     NoteContract.FeedEntry._ID, 
     NoteContract.FeedEntry.NOTES_TITLE, 
     NoteContract.FeedEntry.NOTES_ID, 
     NoteContract.FeedEntry.NOTES_BODY, 

}; 

String sortOrder = 
     NoteContract.FeedEntry.NOTES_ID + " DESC"; 

Cursor cursor = db.query(
     NoteContract.FeedEntry.TABLE_NAME, // The table to query 
     projection,        // The columns to return 
     null,        // The columns for the WHERE clause 
     null,       // The values for the WHERE clause 
     null,          // don't group the rows 
     null,          // don't filter by row groups 
     sortOrder         // The sort order 
); 


if(cursor.getCount()>0) //here i am getting error 

{ 

} 

definiere ich auch Cursor, ich verstehe nicht, warum es nicht funktioniert. Auch

,

ich versuchte if(cursor !=null)

es sagt mir

cursor unknown class

+1

Anweisungen gehen in Methoden oder statische Blöcke. – njzk2

+1

Ich denke, Ihr Code sollte Kompilierungsfehler haben. Könnten Sie den vollständigen Code "GetNotesFromDbTask" posten? – pooyan

+0

Ich habe es gelöst. protected Boolean doInBackground (Void ... params) {return null;} schloss sich automatisch selbst, weil dieser Cursor nicht funktionierte. –

Antwort

1

Code sollte innerhalb einer Methode, die in einer Klasse ist, nicht auf der Ebene der Klassen selbst.

Vorherige Zeilen sind syntaktisch gültig, da es sich um variable Deklarationen und Initialisierungen handelt. Eine if Bedingung ist auf Klassenebene nicht gültig.

0
  class GetNotesFromDbTask extends AsyncTask<Void, Void, Boolean> { 

      @Override 
      protected Boolean doInBackground(Void... params) { 
     SQLiteDatabase db = helpers.getReadableDatabase(); 
      String[] projection = { 
        NoteContract.FeedEntry._ID, 
        NoteContract.FeedEntry.NOTES_TITLE, 
        NoteContract.FeedEntry.NOTES_ID, 
        NoteContract.FeedEntry.NOTES_BODY, 

      }; 

      String sortOrder = 
        NoteContract.FeedEntry.NOTES_ID + " DESC"; 

      Cursor cursor = db.query(
        NoteContract.FeedEntry.TABLE_NAME, // The table to query 
        projection,        // The columns to return 
        null,        // The columns for the WHERE clause 
        null,       // The values for the WHERE clause 
        null,          // don't group the rows 
        null,          // don't filter by row groups 
        sortOrder         // The sort order 
      ); 


      if(cursor.getCount()>0) //here i am getting error 

      { 

      } 
       return null; 
      } 

      @Override 
      protected void onPreExecute() { 
       ProgressDialog dialog = new ProgressDialog(NoteDetail.this); 
       dialog.setTitle("Loading"); 
       dialog.setMessage("Please wait.."); 
       dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
       dialog.setCancelable(false); 
       dialog.show(); 
      } 

      } // this line add asynctask class end 

      protected void onCreate(Bundle savedInstanceState) { 

      GetNotesFromDbTask gnfd=new GetNotesFromDbTask(); 
      //asynctask run onpre ->onbackground->onpost 
gnfd.execute(); 


     }