我是Android编码的新手。我只是想从我的数据库表中逐行显示数据。
这是我创建数据库/表和添加数据的部分。
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);
下面是用于显示数据库值的活动代码(ViewResults.java)。我无法弄清楚我做错了什么。在导出和运行应用程序时,应用程序的其余部分工作正常,但显示数据的部分显示黑屏,应用程序立即退出。虽然如果我使用简单的TextView而不是TableLayout显示数据,它会正常显示。所以数据库部分工作正常,我的TableLayout代码有问题。
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());
}
下面是以表格格式显示结果的XML文件代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>