有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java游标。getCount()给定错误

当我尝试使用游标时,我正在做一个安卓项目。getCount();这给了我一个错误:

cannot resolve symbol getCount();

这是我的密码

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

{

}

我也定义了游标,我不明白它为什么不工作

还有

我试过if(cursor !=null)

它在对我说

cursor unknown class


共 (2) 个答案

  1. # 1 楼答案

    您的代码应该在类内部的方法中,而不是在类级别本身

    前面的行在语法上是有效的,因为它们是变量声明和初始化。if条件在类级别无效

  2. # 2 楼答案

                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();
    
    
            }