2011-09-01 108 views
4

我想要在gridview中顯示數據庫中的數據。 意味着我在我的表中有一個數據(讓它是客戶的詳細信息),我想在gridview(或任何其他控件顯示細節)中顯示它,就像我們在asp.net中完成的一樣。在android中使用gridview從數據庫中顯示數據

解決方案: -

public void FillGrid() { 
    DatabaseHelper dbh = new DatabaseHelper(this); 
    dbh.openDataBase(); 
    Cursor cursor; 
    GridView grv = (GridView) findViewById(R.id.grvData); 
    cursor = dbh.getGridData(); 
    dbh.close(); 
    if (cursor != null) { 
     startManagingCursor(cursor); 

     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
       R.layout.gridlayout, cursor, 
       new String[] { GridTestActivity.KEY_ROW_ID, GridTestActivity.KEY_ID, GridTestActivity.KEY_DESCRIPTION } 
       ,new int[] { R.id.txtGrv_id, R.id.txtGrvid, R.id.txtGrvDescription }); 
     adapter.setViewResource(R.layout.gridlayout); 
     grv.setAdapter(adapter); 
    } 

} 
+0

看來你試圖模擬一個經典的數據感知網格,具有定義良好的列? –

回答

1

這是full example

另外,如果你的開始Android this將是一本適合你的好書。

+0

我想在gridview中顯示一個表格格式的報告 –

0

您可以使用3列設置GridView。它會給你一個表格的樣子。

OR

您可以使用自定義GridView控件的應用程序中使用以下類型代碼..

customergrid.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tab1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<GridView 
    android:id="@+id/grid" 
    android:layout_width="fill_parent" 
    android:layout_height="357dp" 
    android:numColumns="1" 
    android:stretchMode="columnWidth" /> 

<Button 
    android:id="@+id/cancel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="cancel" /> 

</LinearLayout> 

而且,對於每一行使用自定義XML文件..

customerrow.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 
<TableRow> 
<TextView 
android:layout_width="50px" 
android:layout_height="wrap_content" 
android:id="@+id/row_ID" 
android:padding="5px" 
android:layout_weight="1" /> 
<TextView 
android:layout_width="50px" 
android:layout_height="wrap_content" 
android:id="@+id/key_ID" 
android:padding="5px" 
android:layout_weight="1" /> 
<TextView 
android:layout_width="50px" 
android:layout_height="wrap_content" 
android:id="@+id/key_Description" 
android:padding="5px" 
android:layout_weight="1" /> 
</TableRow> 
</TableLayout> 

在onCreate()方法使用customergrid.xml和使用customerrow.xml在網格創建等爲您的代碼..

public void FillGrid() { 
DatabaseHelper dbh = new DatabaseHelper(this); 
dbh.openDataBase(); 
Cursor cursor; 
GridView grv = (GridView) findViewById(R.id.grvData); 
cursor = dbh.getGridData(); 
dbh.close(); 
if (cursor != null) { 
    startManagingCursor(cursor); 

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      R.layout.customerrow, cursor, new String[] { GridTestActivity.KEY_ROW_ID, 
      GridTestActivity.KEY_ID, GridTestActivity.KEY_DESCRIPTION }, new int[] { 
      R.id.txtGrv_id, R.id.txtGrvid, R.id.txtGrvDescription }); 
    adapter.setViewResource(R.layout.gridlayout); 
    grv.setAdapter(adapter); 
} 

} 

你可以在GridView控件從數據庫中的數據現在。

+0

發佈詳細討論的完整項目代碼,它可能對我有幫助..我也在開發一個任務,用於從數據庫獲取數據,並使用警報框更新數據。如果您有任何想法與我分享,我會觸發我的流程。 – MGR

相關問題