populate listview row from more than one database row
I have a listview开发者_Python百科 and i would like to populate a row with several calculations done on multiple database tables rows winch carry the row id as column (group).
So for example one of my database tables has three rows all of winch have a unique row id for one coloumn and also another column (group) which has the same value for all rows allowing them to be grouped together.
What i would like to do is pass a cursor over each row which has the same value in the column group and do a few things on each row like add,minus,subtract etc. and then add the result from each row to a value which i can then attach to the list view.
How can i go about doing this?
You can put the results in an Array
and then do your operations you want on each item. Then you use an arrayAdapter
to put the items in it.
public String[][] getResultSetAsArray(Cursor cursor) {
int numberOfColumns = cursor.getColumnCount();
int numberOfRows = cursor.getCount();
String [][] resultSetAsArray = new String[numberOfColumns][numberOfRows];
for (int currentColumn = 0; currentColumn < numberOfColumns; currentColumn++)
{
cursor.moveToFirst();
for (int currentRow = 0; currentRow < numberOfRows; currentRow++)
{
resultSetAsArray[currentColumn][currentRow] = cursor.getString(currentColumn);
cursor.moveToNext();
}
}
return resultSetAsArray;
}
This method gives you all the results of a cursor back in a two-dimensional array. If you only have one column, you just use the method and put [0]
after it to put it in one array.
Example:
String table = tablename;
String[] columns = {column1, column2};
String orderBy = columnNameToOrder;
Cursor cursor = db.query(table, columns, null, null, null, null, orderBy);
String[] column1Values = dbRetrieval.getResultSetAsArray(tabLabelCursor)[0];
String[] column2Values = dbRetrieval.getResultSetAsArray(tabLabelCursor)[1];
String[][] values = dbRetrieval.getResultSetAsArray(tabLabelCursor); // all the database values in a two-dimensional array.
精彩评论