Skip to content Skip to sidebar Skip to footer

Show Data In Android Using Gridview From Database

I want to display data, from a database, in a gridview. Means I have a data in my table (let it's a customer detail) and I want to show it in gridview (or any other control to disp

Solution 1:

Here's the full example.

Also if your beginning Android this would be a good book for you.


Solution 2:


Solution 3:

You can Set the GridView With 3 Columns. It will give you a look of Table form.

OR

You can use customized GridView for your application Using the Following type of Codes..

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>

And, for each Row use a customized Xml file..

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>

Use customergrid.xml on the Oncreate() method and use customerrow.xml in the Grid creation like as your code..

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);
}

}

You can get the data from the Database on the gridview now.


Post a Comment for "Show Data In Android Using Gridview From Database"