Wednesday, 15 July 2015

java - How to add jsonarray response in linear layout? -



java - How to add jsonarray response in linear layout? -

i getting json response in getting jsonobject , jsonarray. set values jsonobject jsonarray have 2 values each que ans ans. example:

"question": [ { "ans": "test", "que": "what name" }, { "ans": "25-30", "que": "age" } ]

this jsonarray in jsonobject. want set que , ans in textbox. note number of questions not fixed, can 12 or more or less depends on api.

my question how set questions in layout inflater or that? in advance :)

you need utilize inflation this: maintain 1 item.xml hold 2 textviews (or edittext per need.)

item.xml:

<relativelayout android:id="@+id/item_row" android:layout_width="match_parent" android:layout_height="wrap_content" > <textview android:id="@+id/que" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" /> <textview android:id="@+id/ans" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" /> </relativelayout>

now in code:

you need inflate item.xml in loop many items in jsonarray. firstly parse jsonarray , arraylist of question, reply pair:

arraylist<string[]> myarr = new arraylist<string[]>(); (int i=0; < myjsonarray.length(); i++) { jsonobject j = myjsonarray.getjsonobject(i); string[] t = {j.getstring("que"),j.getstring("ans")}; myarr.add[t]; }

pass activity.

now need iterate through arraylist , inflate layout, populate textviews values in map, , add together inflated view in parent layout:

relativelayout container = (relativelayout)findviewbyid(r.id.mylayout); //this container layout (int = 0; < secqaarr.size(); i++){ view item = getlayoutinflater().inflate(r.layout.item, null); textview tque = (textview) findviewbyid(r.id.que); que.setext(secqaarr.get(i)[0]); textview tans = (textview) findviewbyid(r.id.ans); que.setext(secqaarr.get(i)[1]); container.addview(item); }

hope helps.

java android

No comments:

Post a Comment