for loop - Android: Setting ID's for Array of Image Buttons -
i making android application allows users click either plus or minus image button change number displayed in text view.
there 18 of text views , plus , minus image button each text view.
the activity crashes when activity, because of section of code. have been unable produce helpful information when debugging code appreciated, thanks!
at top of activity:
int score = 0; imagebutton addbuttons[] = {(imagebutton)findviewbyid(r.id.hole1up), (imagebutton)findviewbyid(r.id.hole2up), (imagebutton)findviewbyid(r.id.hole3up), (imagebutton)findviewbyid(r.id.hole4up), (imagebutton)findviewbyid(r.id.hole5up), (imagebutton)findviewbyid(r.id.hole6up), (imagebutton)findviewbyid(r.id.hole7up), (imagebutton)findviewbyid(r.id.hole8up), (imagebutton)findviewbyid(r.id.hole9up), (imagebutton)findviewbyid(r.id.hole10up), (imagebutton)findviewbyid(r.id.hole11up), (imagebutton)findviewbyid(r.id.hole12up), (imagebutton)findviewbyid(r.id.hole13up), (imagebutton)findviewbyid(r.id.hole14up), (imagebutton)findviewbyid(r.id.hole15up), (imagebutton)findviewbyid(r.id.hole16up), (imagebutton)findviewbyid(r.id.hole17up), (imagebutton)findviewbyid(r.id.hole18up),}; imagebutton minusbuttons[] = {(imagebutton)findviewbyid(r.id.hole1down), (imagebutton)findviewbyid(r.id.hole2down), (imagebutton)findviewbyid(r.id.hole3down), (imagebutton)findviewbyid(r.id.hole4down), (imagebutton)findviewbyid(r.id.hole5down), (imagebutton)findviewbyid(r.id.hole6down), (imagebutton)findviewbyid(r.id.hole7down), (imagebutton)findviewbyid(r.id.hole8down), (imagebutton)findviewbyid(r.id.hole9down), (imagebutton)findviewbyid(r.id.hole10down), (imagebutton)findviewbyid(r.id.hole11down), (imagebutton)findviewbyid(r.id.hole12down), (imagebutton)findviewbyid(r.id.hole13down), (imagebutton)findviewbyid(r.id.hole14down), (imagebutton)findviewbyid(r.id.hole15down), (imagebutton)findviewbyid(r.id.hole16down), (imagebutton)findviewbyid(r.id.hole17down), (imagebutton)findviewbyid(r.id.hole18down),}; int[] scoreids = new int[] {r.id.score1, r.id.score2, r.id.score3, r.id.score4, r.id.score5, r.id.score6, r.id.score7, r.id.score8, r.id.score9, r.id.score10, r.id.score11, r.id.score12, r.id.score13, r.id.score14, r.id.score15, r.id.score16, r.id.score17, r.id.score18,}; public void setplusbuttons() { for(int = 0; < addbuttons.length; i++) { final int j = i; imagebutton button = addbuttons[i]; button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { textview tv5 = (textview) findviewbyid(scoreids[j]); tv5.settext(score++); } }); } } public void setminusbuttons() { for(int = 0; < minusbuttons.length; i++) { final int j = i; imagebutton button = minusbuttons[i]; button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { textview tv5 = (textview) findviewbyid(scoreids[j]); tv5.settext(score--); } }); } }
my oncreate:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_course); bundle extras = getintent().getextras(); if (extras != null) { chosencoursevalue = extras.getstring("passedchosencourse"); } getsupportactionbar().setdisplayhomeasupenabled(true); asyncapi apithread = new asyncapi(); apithread.execute(); setplusbuttons(); setminusbuttons(); }
section of activity xml
<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="holes" android:id="@+id/hole1" android:layout_marginleft="32dp" android:textcolor="#fff" android:gravity="center" android:background="@drawable/circle" android:height="45dp" android:width="45dp"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="pars" android:id="@+id/hole1par" android:paddingleft="22dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="score" android:id="@+id/score1" android:paddingleft="42dp" /> <imagebutton android:layout_width="30dp" android:layout_height="30dp" android:scaletype="fitxy" android:adjustviewbounds="true" android:croptopadding="false" android:id="@+id/hole1up" android:gravity="center" android:layout_marginleft="32dp" android:layout_margintop="6dp" android:src="@drawable/plus" android:background="@null" /> <imagebutton android:layout_width="32dp" android:layout_height="32dp" android:scaletype="fitxy" android:adjustviewbounds="true" android:croptopadding="false" android:id="@+id/hole1down" android:gravity="center" android:layout_marginleft="26dp" android:layout_margintop="6dp" android:src="@drawable/minus" android:background="@null" /> </linearlayout>
ok.. go stepwise 1 one.
step 1: define views like, include android:onclick
, android:tag
properties in views.
<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="score" android:id="@+id/score1" android:paddingleft="42dp" /> <imagebutton android:layout_width="30dp" android:layout_height="30dp" android:scaletype="fitxy" android:adjustviewbounds="true" android:croptopadding="false" android:id="@+id/hole1up" android:gravity="center" android:layout_marginleft="32dp" android:layout_margintop="6dp" android:src="@drawable/plus" android:background="@null" android:onclick="upbuttononclick" // defines button's onclick in xml android:tag="score1"/> // associated textview id in tag of image button <imagebutton android:layout_width="32dp" android:layout_height="32dp" android:scaletype="fitxy" android:adjustviewbounds="true" android:croptopadding="false" android:id="@+id/hole1down" android:gravity="center" android:layout_marginleft="26dp" android:layout_margintop="6dp" android:src="@drawable/minus" android:background="@null" android:onclick="downbuttononclick" // defines down button's onclick in xml android:tag="score1"/> // associated textview id in tag of image button
so image buttons should have declared android:onclick="upbuttononclick"
, down image buttons should have declared android:onclick="downbuttononclick"
associate textview id android:tag
property.
step 2:
instead of setplusbuttons()
, setminusbuttons()
define upbuttononclick()
, downbuttononclick()
like,
public void upbuttononclick(view imagebutton) { int textviewid = getresources().getidentifier(imagebutton.gettag(), "id", getpackagename()); textview score = (textview) findviewbyid(textviewid); score.settext(score++); }
and
public void downbuttononclick(view imagebutton) { int textviewid = getresources().getidentifier(imagebutton.gettag(), "id", getpackagename()); textview score = (textview) findviewbyid(textviewid); score.settext(score--); }
that's it. don't need declare array of imagebuttons, textview's id , again don't need set on click listener image buttons.
note: sure imagebutton tag should same associated textview id.
Comments
Post a Comment