Count the number of rows in a database from an activity outside the adapter
Trying to get a count of the rows in a database from a separate acti开发者_运维技巧vity. I have been messing with the methods to do so, but I can't seem to instantiate them from another activity if the method is built in the database adapter class. Ideally, I would like the method in the adapter to return an int that I can then use in a number of different activities down the chain. Is there a way to do this that I am not thinking of?
You should make use of the DatabaseUtils.stringForQuery() static method that is already in Android SDK to easily retrieve a value, this example is for String
bot there is also method for Long
stringForQuery(SQLiteDatabase db, String query, String[] selectionArgs)
Utility method to run the query on the db and return the value in the first column of the first row.
Something like
String myString=DatabaseUtils.stringForQuery(getDB(),query,selectionArgs);
精彩评论