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:
Check this post for gridview: http://developer.android.com/resources/tutorials/views/hello-gridview.html
and this for cursor adapter: http://developer.android.com/reference/android/widget/CursorAdapter.html
Hope this helps!
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"?><LinearLayoutxmlns: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" ><GridViewandroid:id="@+id/grid"android:layout_width="fill_parent"android:layout_height="357dp"android:numColumns="1"android:stretchMode="columnWidth" /><Buttonandroid: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"?><TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TableRow><TextViewandroid:layout_width="50px"android:layout_height="wrap_content"android:id="@+id/row_ID"android:padding="5px"android:layout_weight="1" /><TextViewandroid:layout_width="50px"android:layout_height="wrap_content"android:id="@+id/key_ID"android:padding="5px"android:layout_weight="1" /><TextViewandroid: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..
publicvoidFillGrid() {
DatabaseHelperdbh=newDatabaseHelper(this);
dbh.openDataBase();
Cursor cursor;
GridViewgrv= (GridView) findViewById(R.id.grvData);
cursor = dbh.getGridData();
dbh.close();
if (cursor != null) {
startManagingCursor(cursor);
SimpleCursorAdapteradapter=newSimpleCursorAdapter(this,
R.layout.customerrow, cursor, newString[] { GridTestActivity.KEY_ROW_ID,
GridTestActivity.KEY_ID, GridTestActivity.KEY_DESCRIPTION }, newint[] {
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"