Reputation: 3230
My app uses a simple SQLite
database. Everything works fine I can now present a Toast
with a chosen row from the return query using:
{
//etc etc etc...
c.movetofirst();
DisplayTitle(c);
}
public void DisplayTitle(Cursor c)
{
Toast.makeText(main.this,
"id: " + c.getString(0) + "\n" +
"reg: " + c.getString(1) + "\n" +
"type: " + c.getString(2) + "\n",
Toast.LENGTH_LONG).show();
}
I want to prsent all the data in the cursor to the screen using a view but I am not sure how to go about this. Which view should I use? I just wnt a simple looking grid representation which allows me to scroll through each row in the cursor
Thanks in advance.
Upvotes: 0
Views: 1266
Reputation: 8876
How can I show ALL rows in the Cursor?
There are several ways if you are working with a Cursor
, for example see the "moveTo" related methods, here's an example:
Set<Item> items = new HashSet<Item>(c.getCount());
if (c.moveToFirst()) {
do {
Item i = new Item(select(c.getString(0), c.getString(1), c.getString(2));
set.add(i);
} while (c.moveToNext());
}
if (!c.isClosed()) {
c.close();
}
This assumes Item
is a data class that has a constructor with three String
parameters. Going with your code, id, reg, and type. Usually column 0 will be a numeric ID (not getString, but SQLite uses "manifest typing" so it will sort of dyna-type a String/Long from column 0 -- you may want to AUTOINCREMENT and use getLong there though).
One you have a collection of items, you can do anything you want with them to display them. If you want a scrolling/selectable list, then ListView
is an excellent choice. And, you can back it with a CursorAdapter
.
Here's an example of a ListView
that is populated by a CursorAdapter
from an open source project: http://code.google.com/p/and-bookworm/source/browse/trunk/src/com/totsp/bookworm/Main.java (see the inner BookCursorAdapter
class, it should get you pointed in the right direction).
Upvotes: 2