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
What if you remove the
new
keyword when you callgetAll()
? You are not creating a new object there, yourall
list is just getting back the list your return from the method.Creating a
getAll()
method in aSQLiteOpenHelper
class doesn’t help if you want to callgetAll()
on an instance of theSQLite
class.Calling
getAll()
on an instance of theSQLite
class is only valid ifSQLite
or one of its superclasses contains such a method (and most probably,SQLiteOpenHelper
is not a superclass ofSQLite
).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.