我有一个sqlite db我用来存储我的信息,我有四个列标题加权为'1'。我在读取数据时遇到问题,因为它将所有数据整合在一起并覆盖了我的一个列标题。我做错了什么或是否有其他方式我应该显示信息?感谢
以下是代码:
这是视图xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Date" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Time" />
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Hair Wash" />
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Comments" />
</TableRow>
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="get info from db" />
</TableLayout>
</LinearLayout>
这是视图java
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bathreport);
TextView tv = (TextView) findViewById(R.id.textView3);
MyDBAdapter info = new MyDBAdapter(this);
info.open();
String data = info.getData();
info.close();
tv.setText(data);
}
我的适配器的getdata
public String getData() {
String[] columns = new String[] { /*KEY_ROWID,*/ KEY_BATHDATE,
KEY_BATHTIME, KEY_HAIRWASH, KEY_COMMENTS };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
String result = "\n";
//int iRow = c.getColumnIndex(KEY_ROWID);
int iDate = c.getColumnIndex(KEY_BATHDATE);
int iTime = c.getColumnIndex(KEY_BATHTIME);
int iHair = c.getColumnIndex(KEY_HAIRWASH);
int iCom = c.getColumnIndex(KEY_COMMENTS);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + /*c.getString(iRow) + " " +*/ c.getString(iDate)
+ " " + c.getString(iTime) + " " + c.getString(iHair) + " "
+ c.getString(iCom) + "\n";
}
return result;
}
答案 0 :(得分:0)
我想更好的实现是使用SimpleCursorAdapter:
SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
上面的布局应该在一个Horizontal Linear布局中包含4个TextView(类似于你的Table行实现),from是你的列名,to是4 TextViews的ID
然后将此适配器设置为应替换表
的ListView的适配器