开发者

Count the number of rows in a database from an activity outside the adapter

开发者 https://www.devze.com 2023-01-18 06:35 出处:网络
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 th

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消