Can I add data from different tables of the same database into each row of a ListView in Android?
I have a vendor app and I want to add data from one standard items list table and one daily table to the list vie开发者_如何转开发w.
If both tables have the same row format (or at least you're selecting the same type of rows from both tables), you can combine them into a single query with UNION. See the sqlite select query documentation for more info on UNIONs.
I think you mean that the query joins two different tables; is that correct?
If you're using a SimpleCursorAdapter, then you can use a CursorToStringConverter to provide the labels for the ListView. Here's an example:
// choices to be displayed in the AutoCompleteTextView.
adapter.setCursorToStringConverter(new CursorToStringConverter() {
public String convertToString(android.database.Cursor cursor) {
final int columnIndex1 = cursor.getColumnIndexOrThrow("col1");
final String str1 = cursor.getString(columnIndex1);
final int columnIndex2 = cursor.getColumnIndexOrThrow("col2");
final String str2 = cursor.getString(columnIndex2);
return str1 + str2;
}
});
If you want to data from each of the tables to be presented in separate Views (instead of within a single TextView), then you can use a SimpleCursorAdapter.ViewBinder to update the Views. (Here's an example of a ViewBinder. I wrote this to work with a Spinner, but it works the same way with a ListView.)
精彩评论