2012-01-03 34 views
0

我有一个sqlite数据库我用来存储我的信息,我有四个列标题加权到'1'。阅读数据时遇到问题,因为它将所有数据集中在一起,并覆盖了我的一列标题。我做错了什么,或者我有另一种方式显示信息?由于格式化sqlite从数据库读取数据以匹配textview列

下面是代码:

这里是查看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

任何人都有想法? – Intelwalk 2012-01-03 14:09:38

回答

0

我猜更好的实施是使用SimpleCursorAdapter:

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) 

上面的布局应包含在一个水平线性布局4个TextViews(类似于你的表列执行)时,从你的列名,到是4个TextViews的ID

然后将此适配器设置为应该替换您的表的ListView的适配器