android - Displaying SQLite data into TableRows -
i creating app display data sqlite database, though have been successful in displaying data in custom listviews. wondering how able display data in table form, can display first line of data though don't know go here.
this activitymain.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.gin.customlistview.mainactivity"> <tablelayout android:id="@+id/table" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchcolumns="*" android:orientation="vertical"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:text="saved messages" android:textsize="20dp" android:textcolor="#000" android:textstyle="bold" android:gravity="center_horizontal"/> <tablerow android:background="#000000" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="15dp" android:layout_margin="1dp" > <textview android:layout_width="match_parent" android:layout_height="match_parent" android:textsize="15dp" android:textcolor="#000" android:text="phone number" android:layout_margin="1dp" android:background="#ffffff" android:textstyle="bold" android:gravity="center" /> <textview android:layout_width="wrap_content" android:layout_height="match_parent" android:textsize="15dp" android:textcolor="#000" android:text="username" android:layout_margin="1dp" android:background="#ffffff" android:gravity="center" android:textstyle="bold" /> <textview android:layout_width="wrap_content" android:layout_height="match_parent" android:textsize="15dp" android:textcolor="#000" android:text="account number" android:layout_margin="1dp" android:background="#ffffff" android:gravity="center" android:textstyle="bold" android:layout_column="2" /> </tablerow> <tablerow android:id="@+id/tablerow" android:background="#000000" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="1dp"> <textview android:id="@+id/txtphonenumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="15dp" android:textcolor="#000" android:background="#ffffff" android:gravity="center" android:layout_margin="1dp" /> <textview android:id="@+id/txtusername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="15dp" android:textcolor="#000" android:layout_margin="1dp" android:background="#ffffff" android:gravity="center" /> <textview android:id="@+id/txtaccountnumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="15dp" android:textcolor="#000" android:layout_margin="1dp" android:background="#ffffff" android:gravity="center" /> </tablerow> </tablelayout> </linearlayout>
this mainactivity:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); db = new textdatabase(this); txtphonenumber = (textview) findviewbyid(r.id.txtphonenumber); txtusername = (textview) findviewbyid(r.id.txtusername); txtaccountnumber = (textview) findviewbyid(r.id.txtaccountnumber); checkpermission(); showdata(); } public static void showdata() { // databasenumber.clear(); // databaseusername.clear(); // databaseaccountnumber.clear(); cursor data = db.getsms(); if (data.getcount() == 0) { } else { data.movetofirst(); { txtphonenumber.settext(data.getstring(1)); txtusername.settext(data.getstring(2)); txtaccountnumber.settext(data.getstring(3)); // databasenumber.add(data.getstring(1)); // databaseusername.add(data.getstring(2)); // databaseaccountnumber.add(data.getstring(3)); } while (data.movetonext()); } data.close(); // adapters.notifydatasetchanged(); }
thanks in advance , suggestions! :d
try this
<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tablelayout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" android:orientation="vertical"> </tablelayout>
once have created tablelayout create table row layout table_item.xml
<?xml version="1.0" encoding="utf-8"?> <tablerow xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/name" android:layout_weight="0.25" android:gravity="center"/> <textview android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/title" android:layout_weight="0.25" android:gravity="center"/> </tablerow>
inside activity iterate through values retrieved sql , add table
private tablelayout tablelayout; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.table); tablelayout=(tablelayout)findviewbyid(r.id.tablelayout); data.movetofirst(); { view tablerow = layoutinflater.from(this).inflate(r.layout.table_item,null,false); textview name = (textview) tablerow.findviewbyid(r.id.name); textview title = (textview) tablerow.findviewbyid(r.id.title); name.settext(data.getstring(1)); title.settext(data.getstring(2)); tablelayout.addview(tablerow); } while (data.movetonext()); data.close(); }
Comments
Post a Comment