在表格布局中显示sqlite中的数据,

时间:2013-03-07 07:02:37

标签: android xml android-sqlite android-tablelayout

我想将我的数据从sqlite显示到我的表格,我使用xlay创建使用tablelayout,我想怎么做?有什么例子吗?我不知道如何将值传递到表中..是否与listview或textview相同?

下面的

是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textColor="#fcfafa" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="@string/glucose"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#030303" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="500dp"
        android:layout_height="15dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="17dp"
        android:background="#C4BFBF"
        android:orientation="vertical" >
    </LinearLayout>

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="28dp"
        android:layout_height="28dp"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/textView1"
        android:layout_marginLeft="16dp"
        android:contentDescription="@string/btn_home"
        android:onClick="onClickHome"
        android:src="@drawable/homebtn" />

    <Button
        android:id="@+id/imageButton2"
        android:layout_width="60dp"
        android:layout_height="35dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginRight="18dp"
        android:layout_marginTop="10dp"
        android:onClick="onClickAdd"
        android:textSize="14sp"
        android:text="@string/btn_add"/>

    <TableLayout
        android:id="@+id/myTableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="1dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imageButton2"
        android:layout_marginTop="5dp" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="100dp" 
            android:background="@drawable/cell_normalshape">

            <TextView
                android:id="@+id/textView2"
                android:background="@drawable/cell_normalshape"
                android:layout_width="55dp"
                android:text="@string/date"
                android:gravity="center_horizontal"
                android:textColor="#2A0A0A"
                android:textSize="16sp"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textView3"
                android:background="@drawable/cell_normalshape"
                android:layout_weight="0.2"
                android:text="@string/Breakfast"
                android:layout_span="2"
                android:gravity="center_horizontal"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#2A0A0A"
                android:textSize="16sp"/>

            <TextView
                android:id="@+id/textView4"
                android:background="@drawable/cell_normalshape"
                android:layout_width="70dp"
                android:textColor="#2A0A0A"
                android:textSize="16sp"
                android:gravity="center_horizontal"
                android:layout_span="2"
                android:text="@string/Lunch"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView5"
                android:background="@drawable/cell_normalshape"
                android:layout_width="70dp"
                android:textColor="#2A0A0A"
                android:textSize="16sp"
                android:layout_span="2"
                android:gravity="center_horizontal"
                android:text="@string/Dinner"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </TableRow>
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="100dp" 
            android:background="@drawable/cell_normalshape">

         <TextView
                android:id="@+id/textView7"
                android:background="@drawable/cell_normalshape"
                android:layout_width="40dp"
                android:textColor="#2A0A0A"
                android:text=""
                android:textSize="18sp"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textView8"
                android:background="@drawable/cell_normalshape"
                android:layout_weight="0.3"
                android:text="@string/Pre"
                android:gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#2A0A0A"
                android:textSize="16sp"/>

            <TextView
                android:id="@+id/textView9"
                android:background="@drawable/cell_normalshape"
                android:layout_weight="0.3"
                android:layout_width="wrap_content"
                android:textColor="#2A0A0A"
                android:gravity="center_horizontal"
                android:textSize="16sp"
                android:text="@string/Post"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView10"
                android:background="@drawable/cell_normalshape"
                android:layout_width="30dp"
                android:textColor="#2A0A0A"
                android:textSize="16sp"
                android:text="@string/Pre"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView11"
                android:layout_width="40dp"
                android:textColor="#2A0A0A"
                android:textSize="16sp"
                android:text="@string/Post"
                android:gravity="center_horizontal"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView12"
                android:background="@drawable/cell_normalshape"
                android:layout_width="30dp"
                android:text="@string/Pre"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#2A0A0A"
                android:textSize="16sp"/>

            <TextView
                android:id="@+id/textView13"
                android:background="@drawable/cell_normalshape"
                android:text="@string/Post"
                android:layout_width="40dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#2A0A0A"
                android:textSize="16sp"/>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="100dp" 
            android:background="@drawable/cell_normalshape">

            <TextView
                android:id="@+id/textView15"
                android:background="@drawable/cell_normalshape"
                android:layout_width="40dp"
                android:textColor="#2A0A0A"
                android:text=""
                android:textSize="18sp"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textView16"
                android:background="@drawable/cell_normalshape"
                android:layout_weight="0.3"
                android:text=""
                android:layout_width="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#2A0A0A"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/textView17"
                android:background="@drawable/cell_normalshape"
                android:layout_weight="0.3"
                android:layout_width="wrap_content"
                android:textColor="#2A0A0A"
                android:textSize="18sp"
                android:text=""
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView18"
                android:background="@drawable/cell_normalshape"
                android:layout_width="30dp"
                android:textColor="#2A0A0A"
                android:textSize="18sp"
                android:text=""
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView19"
                android:layout_width="40dp"
                android:textColor="#2A0A0A"
                android:textSize="18sp"
                android:text=""
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView20"
                android:background="@drawable/cell_normalshape"
                android:layout_width="30dp"
                android:text=""
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#2A0A0A"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/textView21"
                android:background="@drawable/cell_normalshape"
                android:text=""
                android:layout_width="40dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#2A0A0A"
                android:textSize="18sp" />
        </TableRow>
    </TableLayout>

</RelativeLayout>

这是我的代码..

private SQLiteDatabase database;
private ArrayList<String> result = new ArrayList<String>(); 
private ArrayList<Long> idList = new ArrayList<Long>();
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.glucose_view);
    TableLayout myTable = (TableLayout)findViewById(R.id.myTableLayout);

    DBHelper helper = new DBHelper(this);
    database = helper.getWritableDatabase();

    // view data

    try{
        String query = "select * from " + DBHelper.TABLE_GLUCOSE;

        Cursor c = database.rawQuery(query, null);

        c.moveToFirst();
        c.moveToPosition(3);  // may be two, not sure if the cursor starts at 0 or 1
        while (c.isAfterLast() == false) {
            String tblName = c.getString(1);
            Cursor table = database.rawQuery("SELECT * FROM " + DBHelper.TABLE_GLUCOSE,null);
            table.moveToFirst();
            if (table.getString(table.getColumnIndex("Title")) != null) {
                //  Your code to create table
            }
            c.moveToNext();
        }
}

任何人都可以帮助我..

1 个答案:

答案 0 :(得分:0)

试试这个

  if((cursor.moveToFirst())
      {
      do
       {
      LinearLayout layout=new LinearLayout(context);
      layout.setOrienation(LinearLayout.HORIZONTAL);
       for(int i=0;i<cursor.getColumnCount();i++)
        {
         TextView text=new TextView(context);
         switch(cursor.getType(i))      
         {
          case  Cursor.FIELD_TYPE_INTEGER:
           text.setText(String.valueOf(cursor.getInt(i)))
           break;
       case  Cursor.FIELD_TYPE_STRING:
           text.setText(cursor.getString(i))
           break;
...
..


          }
layout.addView(text);

        }
    tablelayout.addView(layout);
        }
        }whilie(cursor.moveToNext())