Reputation: 9351
having problem with this line of code, compiler error on getInt method. because the variable KEY_HITS is a String and not an int. How do I fix this problem?
total = total + c.getInt(KEY_HITS);
here is more of the code;
public static final String KEY_ROWID="_id";
public static final String KEY_NAME="persons_name";
public static final String KEY_HITS="persons_hits";
public int getTotal() {
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HITS };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
int total = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
total = total + c.getInt(KEY_HITS);
}
return total;
}
Upvotes: 0
Views: 89
Reputation: 42016
Do what errors says because the variable KEY_HITS is a String and not an int
you need to pass integer parameter in getInt(). You can get column's index by getColumnIndex()
total = total + c.getInt(c.getColumnIndex(KEY_HITS));
Upvotes: 1