CuriousCoder CuriousCoder - 18 days ago 5
Android Question

Displaying database table data in Android

I am trying to get the data from SQLite database and display it in a table format in Android. Here is the code for displaying data:

Cursor c=db.getAllTitles();
if(c.moveToFirst())
{
do
{
DisplayTitle(c);
}while(c.moveToNext());
}


And the
DisplayTitle
function looks as shown below:

public void DisplayTitle(Cursor c)
{
txt.setText(c.getString(0));
txt.setTextColor(Color.blue(1));

tr.addView(txt);
tl.addView(tr);
}


Here,
txt
is a
TextView
variable that I have globally declared in the class.
tl
is the
TableLayout
variable and
tr
is
TableRow
variable that I have declared globally in the class.

This doesn't work. However, if I use
Toast
function, the data pulled from the database is getting displayed. Please help.

Here is the link to the entire code:

http://pastebin.me/24d7e33ce334e56087dcb657081684e9

Answer

My guess is that for each data record you need to declare a new table row with a new text view. From the explanation, it doesn't seem that you want to use the same table row and same text view for all data records.

If you're still stuck, please post a more complete code example. Preferably so complete that others can replicate your problem. Also, please explain what "This doesnt work" means.

Comments