i have created database , contents shown in table layout .when insert database data shown in table. working fine.but problem when click on row contents not shown in toast(rather if want show in page).only last row getting clicked column index set @ last shown. in advance.
this xml page
<?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:background="@drawable/gradientb" android:id="@+id/tablelayout1" android:shrinkcolumns="*" android:stretchcolumns="*" > <imageview android:id="@+id/imageview1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scaletype="fitxy" android:src="@drawable/deliverylistbar" /> <tablerow android:id="@+id/tablerow1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" > </tablerow> <tablerow android:id="@+id/tablerow2" android:layout_width="match_parent" android:layout_height="wrap_content" > <textview android:id="@+id/textview0" android:gravity="center|left" android:text="id" android:textcolor="@android:color/black" android:textsize="20dp" android:textstyle="bold" android:typeface="serif" > </textview> <textview android:id="@+id/textview1" android:gravity="center|left" android:text="items" android:textcolor="@android:color/black" android:textsize="20dp" android:textstyle="bold" android:typeface="serif" > </textview> <textview android:id="@+id/textview2" android:gravity="center|left" android:text="location" android:textcolor="@android:color/black" android:textsize="18dp" android:textstyle="bold" android:typeface="serif" > </textview> <textview android:id="@+id/textview3" android:gravity="center|left" android:text="pickby" android:textcolor="@android:color/black" android:textsize="18dp" android:textstyle="bold" android:typeface="serif" > </textview> <textview android:id="@+id/textview4" android:gravity="center|left" android:text="status" android:textcolor="@android:color/black" android:textsize="18dp" android:textstyle="bold" android:typeface="serif" > </textview> </tablerow> </tablelayout>
now sampletableactivitys.java page given here....
public class sampletableactivity extends activity { /** called when activity first created. */ sqlitedatabase database; private static string dbname = "sample.db"; private static string table = "test"; tablelayout tablelayout; tablerow row; textview firstcol; textview secondcol; textview thirdcol; textview fourthcol; textview fifthcol; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); tablelayout=(tablelayout)findviewbyid(r.id.tablelayout1); createdb(); insertvalues(); displaydb(); row.setclickable(true); row.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { toast.maketext(getapplicationcontext(), " "+row.gettag(), toast.length_long).show(); view.setbackgroundcolor(color.white); } }); }
the columns created using this...
private void displaydb() { database=openorcreatedatabase(dbname, context.mode_private, null); if(database!=null) { cursor cursor=database.rawquery("select * "+ table, null); integer index0=cursor.getcolumnindex("id"); integer index1 = cursor.getcolumnindex("items"); integer index2 = cursor.getcolumnindex("location"); integer index3 = cursor.getcolumnindex("name"); integer index4 = cursor.getcolumnindex("status"); if(cursor.getcount()>0) { cursor.movetofirst(); { row=new tablerow(this); row.setid(100); row.settag(cursor.getstring(index0)); row.settag(cursor.getstring(index1)); row.settag(cursor.getstring(index2)); row.settag(cursor.getstring(index3)); row.settag(cursor.getstring(index4)); row.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); /*setting first coloumn parameters*/ firstcol=new textview(this); firstcol.settext(cursor.getstring(index0)); firstcol.settextsize(16); firstcol.settextcolor(color.dkgray); firstcol.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); row.addview(firstcol); //adding coloumn row /*setting second coloumn parameters*/ secondcol=new textview(this); secondcol.settext(cursor.getstring(index1)); secondcol.settextcolor(color.dkgray); secondcol.settextsize(16); secondcol.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); row.addview(secondcol); //adding coloumn row /*setting third coloumn parameters*/ thirdcol=new textview(this); thirdcol.settext(cursor.getstring(index2)); thirdcol.settextcolor(color.dkgray); thirdcol.settextsize(16); thirdcol.setlayoutparams(new layoutparams( layoutparams.wrap_content, layoutparams.wrap_content)); row.addview(thirdcol); //adding coloumn row fourthcol=new textview(this); fourthcol.settext(cursor.getstring(index3)); fourthcol.settextsize(16); fourthcol.settextcolor(color.dkgray); fourthcol.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); row.addview(fourthcol); fifthcol=new textview(this); fifthcol.settext(cursor.getstring(index4)); fifthcol.settextsize(16); fifthcol.settextcolor(color.black); fifthcol.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); row.addview(fifthcol); /*adding row tablelayout*/ tablelayout.addview(row,new tablelayout.layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); }while(cursor.movetonext()); database.close(); } else { toast.maketext(getbasecontext(), "not available", toast.length_long).show(); } } }
the database created here....
private void createdb() { // todo auto-generated method stub try { database=openorcreatedatabase(dbname, context.mode_private, null); database.execsql("create table if not exists "+ table +" (id integer primary key autoincrement not null, items text, location text, name text, status text);"); database.close(); } catch(exception e) { log.e("database creation", "error "+e.tostring()); } }
two things notice :
in
displaydb()
, assigning same id each row. assign unique ids each one. can assign unique id using inside loop :row.setid(increment+10); increment++;
you can initialize
increment
100 or other value, outside loop.
edit : complete code
private void displaydb() { database=openorcreatedatabase(dbname, context.mode_private, null); if(database!=null) { cursor cursor=database.rawquery("select * "+ table, null); **int increment =100;** integer index0=cursor.getcolumnindex("id"); integer index1 = cursor.getcolumnindex("items"); integer index2 = cursor.getcolumnindex("location"); integer index3 = cursor.getcolumnindex("name"); integer index4 = cursor.getcolumnindex("status"); if(cursor.getcount()>0) { cursor.movetofirst(); { row=new tablerow(this); **row.setid(increment + 10); increment++;** row.settag(cursor.getstring(index0)); row.settag(cursor.getstring(index1)); row.settag(cursor.getstring(index2)); row.settag(cursor.getstring(index3)); row.settag(cursor.getstring(index4)); row.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); /*setting first coloumn parameters*/ firstcol=new textview(this); firstcol.settext(cursor.getstring(index0)); firstcol.settextsize(16); firstcol.settextcolor(color.dkgray); firstcol.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); row.addview(firstcol); //adding coloumn row /*setting second coloumn parameters*/ secondcol=new textview(this); secondcol.settext(cursor.getstring(index1)); secondcol.settextcolor(color.dkgray); secondcol.settextsize(16); secondcol.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); row.addview(secondcol); //adding coloumn row /*setting third coloumn parameters*/ thirdcol=new textview(this); thirdcol.settext(cursor.getstring(index2)); thirdcol.settextcolor(color.dkgray); thirdcol.settextsize(16); thirdcol.setlayoutparams(new layoutparams( layoutparams.wrap_content, layoutparams.wrap_content)); row.addview(thirdcol); //adding coloumn row fourthcol=new textview(this); fourthcol.settext(cursor.getstring(index3)); fourthcol.settextsize(16); fourthcol.settextcolor(color.dkgray); fourthcol.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); row.addview(fourthcol); fifthcol=new textview(this); fifthcol.settext(cursor.getstring(index4)); fifthcol.settextsize(16); fifthcol.settextcolor(color.black); fifthcol.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); row.addview(fifthcol); /*adding row tablelayout*/ tablelayout.addview(row,new tablelayout.layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); }while(cursor.movetonext()); database.close(); } else { toast.maketext(getbasecontext(), "not available", toast.length_long).show(); } } }
- in
oncreate()
, settingonclicklistener
row after calling displaydb(), last row has listener attached it. attachonclick()
each row creating , can done in loop create rows.
Comments
Post a Comment