Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Android sqlite read all rows at once

0

44 views

is there way to read all the rows in an sqlite table and display them at once in a textview this is how i read them and it reads line by line ....

//---retrieves all the titles---
    public Cursor getAllTitles() 
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_ISBN,
                KEY_TITLE,
                KEY_PUBLISHER}, 
                null, 
                null, 
                null, 
                null, 
                null);

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;

public class DatabaseActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DBAdapter db = new DBAdapter(this);

        //---get all titles---
        db.open();
        Cursor c = db.getAllTitles();
        if (c.moveToFirst())
        {
            do {          
                DisplayTitle(c);
            } while (c.moveToNext());
        }
        db.close();
    }    
}

 public void DisplayTitle(Cursor c)
    {
        Toast.makeText(this, 
                "id: " + c.getString(0) + "\n" +
                "ISBN: " + c.getString(1) + "\n" +
                "TITLE: " + c.getString(2) + "\n" +
                "PUBLISHER:  " + c.getString(3),
                Toast.LENGTH_LONG).show();        
    } 

asked June 18, 2011 5:30 pm CDT
moe
posted via StackOverflow

1 Answer

0
 

First of all, you might want to look into listviews to easily display a list of data like this.

If your goal really is to display all information in one textview (or toast as you're making now), you could try making one large string, with which you create the toast:

    //---get all titles---
    db.open();
    Cursor c = db.getAllTitles();
    String text = "";
    if (c.moveToFirst())
    {
        do {          
            DisplayTitle(c, text);
        } while (c.moveToNext());
    }
    db.close();
    Toast.makeText(this, text, Toast.LENGTH_LONG).show(); 
}

public void DisplayTitle(Cursor c, String text)
{
    text +=
            "id: " + c.getString(0) + "\n" +
            "ISBN: " + c.getString(1) + "\n" +
            "TITLE: " + c.getString(2) + "\n" +
            "PUBLISHER:  " + c.getString(3);        
} 

answered June 18, 2011 6:23 pm CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions