Currently I have a query that looks at the table and returns the newest row, reads that row and sets the string to the value in the chosen column index.
At the moment I have 9 columns so I have end up making 9 methods for returning each column just to return a string and set it to each individual textview.
Do I make a cursor to return the row and then set the column index when I am setting the textviews. Something like
cursor.getString(1)
This is my current query
public String getName() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_COLUMN2,
KEY_COLUMN3, KEY_COLUMN4, KEY_COLUMN5, KEY_COLUMN6, KEY_COLUMN7, KEY_COLUMN8 };
Cursor c = mDb.query(true, DATABASE_TABLE, columns, null, null, null,
null, "_id DESC", "1");
if (c != null) {
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
You could easily change
getName()to something field-agnostic, such as:And use it like:
A problem with this approach is that you can’t use
getString()for a numerical column, so you wind up with multiple methods for each datatype supported in your table. A way to tackle this is to look into the cursor’sgetType()method or just return a complete data structure from your database access methods. Say you have:If you define a class, say, MyRowMapper you can do something like this:
This is just abstract mailercode, so take the syntaxt with a grain of salt. But hopefully you get the idea.