skip to Main Content

I created following method in SQLiteOpenHelper subclass:

public List<Data> getAll(){
        List<Data> returnList = new ArrayList<>();
        String queryString = "SELECT * FROM " + table;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(queryString, null);
        if(cursor.moveToFirst()){
            do {
                int ID = cursor.getInt(0);
                int soundamp = cursor.getInt(1);
                String date = cursor.getString(2);
                String time = cursor.getString(3);
                double lat = cursor.getDouble(4);
                double lon = cursor.getDouble(5);
                Data newData = new Data(ID, soundamp, date, time, lat, lon);
                returnList.add(newData);
            }while(cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return returnList;
    }

But when I try to use this method in MainActivity on button click like this:

else if(v==viewAll){
            SQLite sql = new SQLite(MainActivity.this);
            List<Data> all = new sql.getAll();
        }

"getAll" gets highlighted red and I get the following message when I hover over it: Cannot resolve symbol ‘getAll’.
Now I have tried Invalidating caches and restarting project but it didn’t work, I have also tried Importing to a new project but that didn’t work either.

2

Answers


  1. What if you remove the new keyword when you call getAll()? You are not creating a new object there, your all list is just getting back the list your return from the method.

    else if(v==viewAll){
                SQLite sql = new SQLite(MainActivity.this);
                List<Data> all = new sql.getAll(); //remove new here
            }
    
    Login or Signup to reply.
  2. Creating a getAll() method in a SQLiteOpenHelper class doesn’t help if you want to call getAll() on an instance of the SQLite class.

    Calling getAll() on an instance of the SQLite class is only valid if SQLite or one of its superclasses contains such a method (and most probably, SQLiteOpenHelper is not a superclass of SQLite).

    EDIT:

    I overlooked the new keyword that @net00 spotted, and of course that makes the attempted call unintellegible to the compiler. new must be followed by a classname, not a method call.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search