Changing font face of a ListView without SimpleAdapter in Android - android

I want to change the font face of the list items in my ListView. I created a custom items for that because I want to let the user see the basic information on the database from that activity. How can I change the fonts of my views?
MainActivity.java
//Method in that fetch the data to list view
private void displayListView() {
final DatabaseHandler db = new DatabaseHandler(this);
Cursor cursor = db.fetchClinicByName("");
// The desired columns to be bound
String[] columns = new String[] { Constants.CLINIC_ID, Constants.CLINIC_NAME, Constants.CLINIC_ADD };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.textviewId, R.id.tv_ClinicName, R.id.tv_ClinicAdd };
simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.attribute_clinic, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.lv_clinic);
listView.setAdapter(simpleCursorAdapter);
registerForContextMenu(listView);
attribute_clinic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clinic Name:"
android:textSize="20sp"
android:layout_marginRight="40dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_ClinicName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="19sp"
android:text="clinic"/>
</TableRow>
<TableRow
android:id="#+id/tableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clinic Address:"
android:textSize="20sp"
android:layout_marginRight="20dp"
android:textStyle="bold"
android:paddingBottom="5dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/tv_ClinicAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="19sp"
android:text="clinicAdd"
android:paddingBottom="5dp"
android:layout_marginBottom="5dp" />
</TableRow>
</LinearLayout>
<TextView
android:id="#+id/textviewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="114dp"
android:layout_marginTop="16dp"
android:visibility="gone" />

I have not actually tried this yet, but I believe this should work.
You need to override SimpleCursorAdapter's setViewText(). It would be something like this when your fonts are inside of your assets/fonts folder.
private void displayListView() {
final DatabaseHandler db = new DatabaseHandler(this);
Cursor cursor = db.fetchClinicByName("");
// The desired columns to be bound
String[] columns = new String[] { Constants.CLINIC_ID, Constants.CLINIC_NAME, Constants.CLINIC_ADD };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.textviewId, R.id.tv_ClinicName, R.id.tv_ClinicAdd };
simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.attribute_clinic,
cursor, columns, to, 0) {
#Override
public void setViewText(TextView v, String text) {
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/yourfont.ttf");
v.setTypeface(face);
v.setText(text);
}
};
ListView listView = (ListView) findViewById(R.id.lv_clinic);
listView.setAdapter(simpleCursorAdapter);
registerForContextMenu(listView);

Related

How can I change the visibility of an item inside Simple Cursor Adapter that not comes from "to" parameter

I have a SimpleCursorAdapter that contains some textViews and ImageViews his structure is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/holo_red_dark"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ImgViewIconFoodList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:src="#drawable/ic_spice" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/ImgViewIconFoodList"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:id="#+id/txtViewFoodCategoryList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category"
android:textColor="#949494"
android:textSize="12dp" />
<TextView
android:id="#+id/txtViewFoodNameList"
style="#style/foodListText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="15dp" />
</LinearLayout>
<ImageView
android:id="#+id/imgAddedCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:adjustViewBounds="false"
android:visibility="invisible"
app:srcCompat="#drawable/ic_shopping_cart_verified_symbol" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
When I'm populating most of items (textviews,imgviews,etc... ) are from a db and works well when are in parameters "from" and "to" in the SimpleCursorAdapter creation.
But my problem is when I need to acces to the (ImageView) id/imgAddedCart .
This ImageView is not in the "to" parameter but i need to interactuate with this.
I wanna change his poperty visibility in function of a condition(if
(id_alimento == codigo))
but i can't do this because I don't know how to acces to this item.
Can someone help me ?
lots of thanks.
String[] from = new String[]{MyDBAdapter.KEY_NOMBRE,MyDBAdapter.KEY_CAT,MyDBAdapter.KEY_ID_CAT};
int[] to = new int[]{R.id.txtViewFoodNameList,R.id.txtViewFoodCategoryList,R.id.ImgViewIconFoodList};
private void fillData() {
//obtain all codes for principal cursor
myCursor = mDBHelper.fetchAllCodes();
//obtain all codes for other cursor to compare with principal
myShoppingListCursor = mDBHelper.fetchShoppingList();
getActivity().startManagingCursor(myShoppingListCursor);
getActivity().startManagingCursor(myCursor);
SimpleCursorAdapter codes = new SimpleCursorAdapter(rootView.getContext(),R.layout.row,myCursor,from,to,0);
codes.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
String id_alimento = cursor.getString(cursor.getColumnIndex("_id"));
if (myShoppingListCursor.moveToFirst()) {
//Recorremos el cursor hasta que no haya más registros
do {
String codigo = myShoppingListCursor.getString(myShoppingListCursor.getColumnIndex("id_alimento"));
if (id_alimento == codigo){
// HERE I WANNA CHANGE THE #+id/imgAddedCart visibility property to Visible
}
} while(myShoppingListCursor.moveToNext());
}
if (viewId == R.id.txtViewFoodNameList) {((TextView) view).setText(cursor.getString(columnIndex)); }
if (viewId == R.id.txtViewFoodCategoryList){((TextView) view).setText(cursor.getString(columnIndex));};
if (viewId == R.id.ImgViewIconFoodList){
icName = "ic_oil";
}
Context c = getActivity().getApplicationContext();
int id = c.getResources().getIdentifier("drawable/"+icName, null, c.getPackageName());
((ImageView) view).setImageResource(id);
}
return true;
}});
lv.setAdapter(codes);
}

How can i align content left and right into the listview in android?

code:
{
private ListView l1;
Dbhelper mydb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.e_view);
l1 = (ListView)findViewById(R.id.listView1);
mydb = new Dbhelper(this);
ArrayList<Map<String, String>> list = mydb.getAllexps();
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
//adding it to the list view.
l1.setAdapter(adapter);
l1.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),Ex_add.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
plz help me out
I need to display amount right side of the list and other things to left side.
U need to create custom Listview
refere this link and u will get idea what i m telling u ;)
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92
In the following line you are using android's default list item layout android.R.layout.simple_list_item_2.
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
Instead of using this default layout you can define your own custom layout by putting a XML file into your layout folder. In this custom layout file you can align the content as your need.
you should use this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/pop_up_child_container_topview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="100" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50" >
<TextView
android:id="#+id/pop_up_child_inventoryno_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="#string/str_fullhistory_popup_child_inv_no"
android:textSize="#dimen/list_title_textsize" />
<TextView
android:id="#+id/pop_up_child_inventoryno_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/pop_up_child_inventoryno_title"
android:padding="2dp"
android:text="30vuasou3209"
android:textSize="#dimen/subtext_textsize" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="50"
android:gravity="right" >
<TextView
android:id="#+id/pop_up_child_quantity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="2dp"
android:text="#string/str_fullhistory_popup_child_qty"
android:textSize="#dimen/list_title_textsize" />
<TextView
android:id="#+id/pop_up_child_quantity_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/pop_up_child_quantity_title"
android:padding="2dp"
android:text="30vuasou3209"
android:textSize="#dimen/subtext_textsize" />
</RelativeLayout>
</LinearLayout>
<View
android:id="#+id/popup_child_seperator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/pop_up_child_container_topview"
android:background="#color/fleet_guard_seperator_grey" />
<LinearLayout
android:id="#+id/pop_up_child_container_balance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/popup_child_seperator"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="50"
android:gravity="right" >
<TextView
android:id="#+id/pop_up_child_balance_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/pop_up_child_balance_tv"
android:text="#string/str_fullhistory_popup_child_balance"
android:textSize="#dimen/list_title_textsize" />
<TextView
android:id="#+id/pop_up_child_balance_tv"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="12"
android:textSize="#dimen/list_title_textsize" />
</RelativeLayout>
</LinearLayout>
Hi Om please use linearlayout with horizontal and add textview and set custom xml file to your adapter

SimpleCursorAdapter is not populating ListView

The SimpleCursorAdapter is not populating my ListView, no error, just nothing happens.
Could someone help me find the error?
1) Layout with ListView component (tab_frag1_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:orientation="vertical" >
<ListView
android:id="#+id/lvVehicle"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
2) Layout to represent row (vehicle_row.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"
android:padding="10dip" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dip" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_arrow_right"
android:contentDescription="#null"
android:adjustViewBounds="true"
android:layout_marginRight="3dip"
android:layout_gravity="center_vertical" />
<TextView
android:id="#+id/plate"
style="#style/vehicleDefaultFont.plate"
android:text="#string/label_text_view" />
<TextView
android:id="#+id/hyphen"
android:text="#string/label_hyphen" />
<TextView
android:id="#+id/model"
android:text="#string/label_text_view" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dip" >
<TextView
android:id="#+id/driver"
style="#style/vehicleDefaultFont.driver"
android:layout_span="4"
android:text="#string/label_text_view" />
</TableRow>
</TableLayout>
3) Fragment Class:
public class Tab1Fragment extends Fragment {
String TAG = getClass().getName();
private VehicleDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_frag1_layout, container, false);
dbHelper = new VehicleDbAdapter(getActivity());
dbHelper.open();
dbHelper.deleteAllVehicles();
dbHelper.insertVehicles();
Cursor cursor = dbHelper.fetchAllVehicles();
if(cursor != null && cursor.getCount() > 0) {
String[] columns = new String[] {
VehicleDbAdapter.KEY_MODEL,
VehicleDbAdapter.KEY_PLATE,
VehicleDbAdapter.KEY_DRIVER
};
int[] to = new int[] {
R.id.model,
R.id.plate,
R.id.driver,
};
dataAdapter = new SimpleCursorAdapter(
view.getContext(),
R.layout.vehicle_row,
cursor,
columns,
to,
0);
ListView listView = (ListView) view.findViewById(R.id.lvVehicle);
listView.setAdapter(dataAdapter);
Log.i(TAG, "Cursor = " + cursor.getCount());
} else {
Log.i(TAG, "Cursor = " + cursor.getCount());
}
return view;
}
}
Curiously:
My cursor contains an _id field.
I doublechecked my cursor and it has 7 rows.
07-21 01:30:22.215: I/ Cursor = 7
I tried using the layout to the generic android list layout, but nothing doing :(
dataAdapter = new SimpleCursorAdapter(
view.getContext(),
android.R.layout.simple_list_item_1,
cursor,
columns,
to,
0);
Any help is welcome.
The TextViews that you have defined in vehical_row.xml: #+id/plate, #+id/hyphen, #+id/model and #+id/driver are all missing the layout_width and the layout_height attributes. These are required.
For now, set these attributes as "wrap_content". For example:
<TextView
android:id="#+id/plate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/vehicleDefaultFont.plate"
android:text="#string/label_text_view" />
Do the above for the other three as well. This should work for you.
I think you have problem at XMl. Check id of your listview.
android:id="#+android:id/mainListView">

ListView Onclick?

i have a question about adding a onClick to the a ListView, i have tried to follow the Android NotePad tutorial as much as posible but with my layout i dont quite understand how to add it in.
this is the activity class and it simpley populates the view
public class RemoveForm extends ListActivity {
private DB_Conn DB_Handler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DB_Handler = new DB_Conn(this);
DB_Handler.open();
fillData();
}
protected void onListItemClick(View view) {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = DB_Handler.fetchAll();
startManagingCursor(c);
String[] from = new String[] { DB_Conn.KEY_MODULECODE,
DB_Conn.KEY_DAY,
DB_Conn.KEY_TIME,
DB_Conn.KEY_DURATION,
DB_Conn.KEY_TYPE,
DB_Conn.KEY_ROOM,DB_Conn.KEY_ROWID };
int[] to = new int[] { R.id.MODULECODE,
R.id.DAY,
R.id.TIME,
R.id.DURATION,
R.id.TYPE,
R.id.ROOM,
R.id.ROWID};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter entry =
new SimpleCursorAdapter(this, R.layout.list, c, from, to);
setListAdapter(entry);
}
}
and this is the xml for each record.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:baselineAligned="false">
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout2" android:layout_height="wrap_content"
android:layout_width="match_parent" android:layout_weight="1"
android:baselineAligned="false">
</LinearLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:id="#+id/relativeLayout1" android:layout_width="match_parent">
<TextView android:id="#+id/ROWID"
android:text="TextView"
android:layout_marginRight="5dip"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></TextView>
<TextView android:id="#+id/MODULECODE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_toRightOf="#+id/ROWID"
android:layout_alignTop="#+id/ROWID"></TextView>
<TextView android:layout_marginLeft="25dip"
android:id="#+id/DAY"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_below="#+id/MODULECODE"
android:layout_alignLeft="#+id/MODULECODE"></TextView>
<TextView android:id="#+id/TIME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_toRightOf="#+id/DAY"
android:layout_alignTop="#+id/DAY"
android:layout_alignBottom="#+id/DAY"></TextView>
<TextView android:id="#+id/DURATION"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_toRightOf="#+id/TIME"
android:layout_alignTop="#+id/TIME"
android:layout_alignBottom="#+id/TIME"></TextView>
<TextView android:id="#+id/ROOM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_toRightOf="#+id/MODULECODE"
android:layout_alignTop="#+id/MODULECODE"
android:layout_alignBottom="#+id/MODULECODE"></TextView>
<TextView android:id="#+id/TYPE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_toRightOf="#+id/ROOM"
android:layout_alignTop="#+id/ROOM"
android:layout_alignBottom="#+id/ROOM"></TextView>
</RelativeLayout>
if anyone can help i'd be so grateful right now.
You have to override the,
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}

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 display details) same as we done in asp.net.
Solution:--
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);
}
}
Here's the full example.
Also if your beginning Android this would be a good book for you.
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!
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.

Categories

Resources