android - Button OnClicklistener from dynamically genereated Buttons -
i have problem build correct onclicklistener buttons generate @ runtime. found threads here on stackoverflow after many tries don't working.
i have methode builds gui textview in left "column" , in right "column" x buttons. each button has other link should open onclick. don't know link before it's created @ runtime.
here code actuall try. in case everytime link of last generated button. if click on first one, second 1 .... it's everytime same link.
hope there solution it!
private void createnewview (string jsoninputbeacon, string jsoninputconfig){ tablelayout tablelayout = new tablelayout(this); tablelayout.setlayoutparams(new tablerow.layoutparams(tablerow.layoutparams.match_parent, tablerow.layoutparams.match_parent)); tablelayout.setstretchallcolumns(true); try { final jsonarray jsonarraybeacon = new jsonarray(jsoninputbeacon); final jsonarray jsonarrayconfig = new jsonarray(jsoninputconfig); int patientencounter = 1; for(int jsonobjectcounterbeacon = 0; jsonobjectcounterbeacon < jsonarraybeacon.length(); jsonobjectcounterbeacon ++ ){ final jsonobject objectbeacon = jsonarraybeacon.getjsonobject(jsonobjectcounterbeacon); tablerow row = new tablerow(this); textview outputleft = new textview(this); outputleft.settext("patient " + patientencounter + ":\n" + "name: " + objectbeacon.getstring("surname") + ", " + objectbeacon.getstring("firstname") + "\n" + "geb-datum: " + objectbeacon.getstring("birthdate")); row.addview(outputleft); (int jsonobjectcounterconfig = 0; jsonobjectcounterconfig < jsonarrayconfig.length(); jsonobjectcounterconfig++){ final jsonobject objectconfig = jsonarrayconfig.getjsonobject(jsonobjectcounterconfig); tablerow rowright = new tablerow(this); button buttonright = new button(getapplicationcontext()); buttonright.settext(objectconfig.getstring("name")); final string myurl = objectconfig.getstring("link"); buttonright.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent browserintent = new intent(intent.action_view,uri.parse(myurl)); startactivity(browserintent); } }); rowright.addview(buttonright); row.addview(rowright); } tablelayout.addview(row); patientencounter +=1; } } catch (jsonexception e) { e.printstacktrace(); } setcontentview(tablelayout); }
but in case everytime link of last generated button. if click on first one, second 1 .... it's everytime same link
because myurl
contains value assigned last iteration of for-loop
.
use settag/gettag
method of buttonright
url according button
click. like:
set value using settag
:
final string myurl = objectconfig.getstring("link"); buttonright.settag(myurl);
and myurl
value using v
parameter of onclick
method:
intent browserintent = new intent(intent.action_view, uri.parse(v.gettag().tostring()));
Comments
Post a Comment