android - Building table layout dynamically at run time from XML -
new android, have view below actual values.
my code dummy values below:
<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margintop="10dp"> <tablerow android:background="#607d8b" android:padding="5dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="title" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="due on" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="" /> </tablerow> <tablerow android:background="#eceff1" android:padding="5dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="access knowledge" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="12 feb 2016" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="renew" /> </tablerow> <tablerow android:background="#eceff1" android:padding="5dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="achivement" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="16 feb 2016" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="renew" /> </tablerow> <tablerow android:background="#eceff1" android:padding="5dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="god of small things" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="22 feb 2016" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="renew" /> </tablerow> <button android:text="renew all" /> </tablelayout>
i getting values xml webservices , parsing show in table view.
i using documentbuilderfactory parse xml. here xml file in string str.
builder = documentbuilderfactory.newinstance().newdocumentbuilder(); inputsource src = new inputsource(); src.setcharacterstream(new stringreader(str)); document doc = builder.parse(src); string title = `doc.getelementsbytagname("title").item(0).gettextcontent();`
normally use
titletxt.settext("title: " + title); publishertxt.settext("publisher: " + publisher)
to show results in view.
i have multiple questions: q1 modifications needed in layout file show - actual values , not dummy values? q2 how array values in xml parsing show in layout file? q3 how show xml parsed results in table view - dynamically @ runtime?
these related queries, had put in same question. of may sound simple, being new android, not able figure out. tried find sample code not find. not sure if using right keywords search looking for.
any links sample code or exact keywords search issue solution or suggestions appreciated.
thanks in advance.
in order add new views at runtime, can use view.add method. so, in order make above table "dynamic", can alter layout file contain tablelayout. , later, @ runtime programmatically add tablerows tablelayout. basically, there two options how add tablerow then. first, can construct tablerow, e.g.:
textview tvtitle = new textview(...); textview tvdueon = new textview(...); button btnrenew = new textview(...); tablerow tr = new tablerow(...); tr.add(tvtitle); tr.add(tvdueon); tr.add(btnrenew); tablelayout lyttable = (tablelayout)findviewbyid(...); lyttable.add(tr);
this minimal version, adding views weight, layout_width , layout_height, can achieve this.
second, can design tablerow in layout xml file, can load , add tablelayout @ runtime using layoutinflater.
lyttablerow.xml
<tablerow android:id="+@id/lyttablerow" android:background="#607d8b" android:padding="5dp"> <textview android:id="+@id/tvtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <textview android:id="+@id/tvdueon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <button android:id="+@id/btnrenew" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="" /> </tablerow>
code in activity
tablelayout lyttable = (tablelayout)findviewbyid(r.id.lyttable); view tablerow = getlayoutinflater().inflate(r.layout.lyttablerow, null); textview tvtitle = (textview)tablerow.findviewbyid(r.id.tvtitle); textview tvdueon = (textview)tablerow.findviewbyid(r.id.tvdueon); button btnrenew = (button)tablerow.findviewbyid(r.id.btnrenew); tvtitle.settext(...);
both code-parts can put loop instance, in order add multiple rows - pay attention set tags/ids views using view.settag in order able distinguish them later in button click listener instance.
concerning data, depending on kind of xml parser use, can loop through every item in data , add them tablelayout using 1 of options shown above. according question, use dom parser. can loop through items using nodelist retrieved doc.getelementsbytagname("parentnodename")
parentnodename element name contains "title", "dueon" etc.
Comments
Post a Comment