Show data in android using gridview from database

I want to display data from a database in a grid. So I have data in my table (let it be client details), and I want to show it in gridview (or any other control for displaying details) in the same way as in asp.net.

Decision: -

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); } } 
+4
source share
3 answers

Here is a complete example .

Also, if your starting Android this one will be a good book for you.

+1
source

You can set a gridview with 3 columns. This will give you a table view.

OR

You can use custom 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 line, use a custom 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 in the Oncreate () method and use customerrow.xml in creating the Grid 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); } } 

Now you can get data from the gridview database.

0
source

All Articles