0

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.

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

I already have a method for populating the cursor with all rows in the table, what I am asking for is how to present this to the screen. I want to use a view to present all data in the cursor onto the screen. sorry If i was not very clear!
I updated the answer, after you updated the question ;). If you want a simple looking scrollable "grid," and you already have the Cursor, then ListView/CursorAdapter is the ticket.
Thanks you for the link im taking a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.