Display SQLite table data row by row in Android using TableLayout dynamically



I'm new to android coding. I simply want to display data row by row from my database table.


Here is the part where I have created database/table and added data.



SQLiteDatabase glucoDB = openOrCreateDatabase("glucoDB",MODE_PRIVATE,null);
glucoDB.execSQL("CREATE TABLE IF NOT EXISTS Results(Timestamp VARCHAR, Level_using_hsv_range VARCHAR,Level_using_hsv_gradient VARCHAR,Level_using_lab_range VARCHAR,Level_using_lab_gradient VARCHAR);");
String query=new String("INSERT INTO Results VALUES ('" + currentDateTimeString + "', '" + str + "mg/dL', '" + glucose[1] + "mg/dL', '" + gvalue + "-" + largeg + "mg/dL', '" + LabRes2 + "mg/dL');");
glucoDB.execSQL(query);


Below is the activity code (ViewResults.java) for displaying database values.I can't figure out what I'm doing wrong. On exporting and running the app, the rest of the app works fine but the part where data is displayed shows a black screen and app exits instantly. Although if I display data using simple TextView and not TableLayout, it is displayed normally. So database part is working fine, there's something wrong with my TableLayout code.



public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_page);
int i=0,j=0;
SQLiteDatabase Glucodb = openOrCreateDatabase("glucoDB",MODE_PRIVATE,null);
Cursor resultSet = Glucodb.rawQuery("Select * from Results",null);
resultSet.moveToFirst();

TableLayout l1 = (TableLayout) findViewById(R.id.table2);
l1.setStretchAllColumns(true);
l1.bringToFront();

do
{ TableRow tr = new TableRow(getBaseContext());

TextView t1 = new TextView(getBaseContext());
TextView t2 = new TextView(getBaseContext());
TextView t3 = new TextView(getBaseContext());
TextView t4 = new TextView(getBaseContext());
TextView t5 = new TextView(getBaseContext());
t1.setBackgroundResource(R.drawable.border);
t1.setText(resultSet.getString(0));
t2.setText(resultSet.getString(1));
t3.setText(resultSet.getString(2));
t4.setText(resultSet.getString(3));
t5.setText(resultSet.getString(5));

tr.addView(t1);
tr.addView(t2);
tr.addView(t3);
tr.addView(t4);
tr.addView(t5);

l1.addView(tr);
}while(resultSet.moveToNext());

}


Below is the code of XML file associate with displaying results in table format.



<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TableLayout
android:id="@+id/table2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_weight="0.80"
android:stretchColumns="*" >

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
android:gravity="left"
android:text="Timestamp"
android:background="@drawable/border"
android:textStyle="bold" />

<TextView
android:gravity="center"
android:text="Gvalue1"
android:background="@drawable/border"
android:textStyle="bold" />

<TextView
android:gravity="center"
android:text="Gvalue2"
android:background="@drawable/border"
android:textStyle="bold" />

<TextView
android:gravity="center"
android:text="Gvalue3"
android:background="@drawable/border"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:text="Gvalue4"
android:background="@drawable/border"
android:textStyle="bold" />
</TableRow>
</TableLayout>

</LinearLayout>

No comments:

Post a Comment