0

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

1
  • Folks could probably guess what the layout xml looks like, but how about you show yours, just the same? Commented Apr 7, 2011 at 20:47

2 Answers 2

1

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.

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

2 Comments

pastebin.me/24d7e33ce334e56087dcb657081684e9 .. Pls view the complete code here.... IT doesnt work means that i get the "Your application stopped working unexpectedly" error message ...
Did you make the change I suggested? Are you still having a problem? Are you able to post the complete code, including the layout XML and the Manifest, as requested?
0

try this code...

public Cursor getAll()  
{
   return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
   KEY_MOVES, KEY_TIME}, null, null, null, null, null);
}

after that use this code in main activity

private void getall()  
{

   db.open();

   Cursor c = db.SortAllRows();
   int i=1;
   if (c.moveToFirst())   
   {        
      do
      {
         DisplayContact(c,i++);
      } while (c.moveToNext());
   }
   c.close();
   db.close();
}

public void DisplayContact(Cursor c,int row )   
{            
 String name11 = c.getString(1) + c.getString(2) + c.getString(3);           
 tv1.setText(name11 );
}

Comments

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.