Next button for items xml file - android -
i have xml file 500 texts. have button take sentace random , show it. want add 2 more buttons next , back. take every sentace. code rando button.
final textview tv = (textview) findviewbyid(r.id.quote); resources res = getresources(); mystring = res.getstringarray(r.array.quote); string q = mystring[rgenerator.nextint(mystring.length)]; tv.settext(q); button btn = (button) findviewbyid(r.id.btn); btn.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { mystring = getresources().getstringarray(r.array.quote); string q = mystring[rgenerator.nextint(mystring.length)]; tv.settext(q); } }); }
what code these buttons? thank you
all code:
import java.util.random; import android.app.activity; import android.content.context; import android.content.res.resources; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.view; import android.widget.button; import android.widget.linearlayout; import android.widget.textview; import com.google.android.gms.ads.adrequest; import com.google.android.gms.ads.adsize; import com.google.android.gms.ads.adview; public class mainactivity extends actionbaractivity { context context; private string[] mystring; private static final random rgenerator = new random(); private adview adview; int index = 0; private static final string ad_unit_id = "ca-app-pub-7628432187347131/3277094808"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); getsupportactionbar().hide(); // create ad. adview = new adview(this); adview.setadsize(adsize.smart_banner); adview.setadunitid(ad_unit_id); context = this; linearlayout layout = (linearlayout) findviewbyid(r.id.ads_lin); layout.addview(adview); adrequest adrequest = new adrequest.builder() .addtestdevice(adrequest.device_id_emulator) .addtestdevice("5b895a3cc0ca50d56506e300a4c8342b") .addtestdevice("d039292a1f434c999b21503d63d6fd88") .addtestdevice("test_emulator").build(); // start loading ad in background. adview.loadad(adrequest); // start loading ad in background. final textview tv = (textview) findviewbyid(r.id.quote); resources res = getresources(); mystring = res.getstringarray(r.array.quote); string q = mystring[rgenerator.nextint(mystring.length)]; tv.settext(q); button btn = (button) findviewbyid(r.id.btn); button btnnext = (button) findviewbyid(r.id.btnnext); button btnpreview = (button) findviewbyid(r.id.btnpreview); btn.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { mystring = getresources().getstringarray(r.array.quote); index = rgenerator.nextint(mystring.length); string q = mystring[index]; tv.settext(q); } }); btnnext.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { string q = mystring[index++]; tv.settext(q); } }); btnpreview.setonclicklistener(new view.onclicklistener(){ public void onclick(view v){ string q = mystring[index--]; tv.settext(q); } }); } @override public void onresume() { super.onresume(); if (adview != null) { adview.resume(); } } @override public void onpause() { if (adview != null) { adview.pause(); } super.onpause(); } /** called before activity destroyed. */ @override public void ondestroy() { // destroy adview. if (adview != null) { adview.destroy(); } super.ondestroy(); } }
no errors, when run app, stopped.
this logcat:
09-20 17:24:53.341: e/dalvikvm(7651): adjustadaptivecoef max=6291456, min=1572864, ut=568 09-20 17:24:54.152: e/androidruntime(7651): fatal exception: main 09-20 17:24:54.152: e/androidruntime(7651): java.lang.runtimeexception: unable start activity componentinfo{com.example.stiaica/com.example.stiaica.mainactivity}: java.lang.classcastexception: android.widget.imageview cannot cast android.widget.button 09-20 17:24:54.152: e/androidruntime(7651): @ android.app.activitythread.performlaunchactivity(activitythread.java:2245) 09-20 17:24:54.152: e/androidruntime(7651): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2299) 09-20 17:24:54.152: e/androidruntime(7651): @ android.app.activitythread.access$700(activitythread.java:150) 09-20 17:24:54.152: e/androidruntime(7651): @ android.app.activitythread$h.handlemessage(activitythread.java:1280) 09-20 17:24:54.152: e/androidruntime(7651): @ android.os.handler.dispatchmessage(handler.java:99) 09-20 17:24:54.152: e/androidruntime(7651): @ android.os.looper.loop(looper.java:137) 09-20 17:24:54.152: e/androidruntime(7651): @ android.app.activitythread.main(activitythread.java:5283) 09-20 17:24:54.152: e/androidruntime(7651): @ java.lang.reflect.method.invokenative(native method) 09-20 17:24:54.152: e/androidruntime(7651): @ java.lang.reflect.method.invoke(method.java:511) 09-20 17:24:54.152: e/androidruntime(7651): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) 09-20 17:24:54.152: e/androidruntime(7651): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) 09-20 17:24:54.152: e/androidruntime(7651): @ dalvik.system.nativestart.main(native method) 09-20 17:24:54.152: e/androidruntime(7651): caused by: java.lang.classcastexception: android.widget.imageview cannot cast android.widget.button 09-20 17:24:54.152: e/androidruntime(7651): @ com.example.stiaica.mainactivity.oncreate(mainactivity.java:59) 09-20 17:24:54.152: e/androidruntime(7651): @ android.app.activity.performcreate(activity.java:5283) 09-20 17:24:54.152: e/androidruntime(7651): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1097) 09-20 17:24:54.152: e/androidruntime(7651): @ android.app.activitythread.performlaunchactivity(activitythread.java:2209) 09-20 17:24:54.152: e/androidruntime(7651): ... 11 more 09-20 17:24:54.252: e/googleplayservicesutil(7651): google play services resources not found. check project configuration ensure resources included.
i tried change actionbaractivity activity more errors comes up.
now working, buttons working. there problem, when reached @ last sentance , press next button app stopped. same thing first sentance.
logcat:
09-20 18:08:56.915: e/dalvikvm(20667): adjustadaptivecoef max=4194304, min=1048576, ut=568 09-20 18:08:58.937: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:08:59.508: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:08:59.548: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:08:59.568: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:09:31.949: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:09:32.020: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:09:32.040: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:09:32.040: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:10:03.740: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:10:03.841: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:10:03.861: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:10:03.871: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:10:34.871: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:10:34.931: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:10:34.961: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:10:34.961: e/googleplayservicesutil(20667): google play services resources not found. check project configuration ensure resources included. 09-20 18:10:40.226: e/androidruntime(20667): fatal exception: main 09-20 18:10:40.226: e/androidruntime(20667): java.lang.arrayindexoutofboundsexception: length=593; index=593 09-20 18:10:40.226: e/androidruntime(20667): @ com.example.stiaica.mainactivity$2.onclick(mainactivity.java:73) 09-20 18:10:40.226: e/androidruntime(20667): @ android.view.view.performclick(view.java:4432) 09-20 18:10:40.226: e/androidruntime(20667): @ android.view.view$performclick.run(view.java:18338) 09-20 18:10:40.226: e/androidruntime(20667): @ android.os.handler.handlecallback(handler.java:725) 09-20 18:10:40.226: e/androidruntime(20667): @ android.os.handler.dispatchmessage(handler.java:92) 09-20 18:10:40.226: e/androidruntime(20667): @ android.os.looper.loop(looper.java:137) 09-20 18:10:40.226: e/androidruntime(20667): @ android.app.activitythread.main(activitythread.java:5283) 09-20 18:10:40.226: e/androidruntime(20667): @ java.lang.reflect.method.invokenative(native method) 09-20 18:10:40.226: e/androidruntime(20667): @ java.lang.reflect.method.invoke(method.java:511) 09-20 18:10:40.226: e/androidruntime(20667): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) 09-20 18:10:40.226: e/androidruntime(20667): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) 09-20 18:10:40.226: e/androidruntime(20667): @ dalvik.system.nativestart.main(native method)
you need store generated random in index variable
to move next increment variable
to move decrement it
edit
here how :
public class mainactivity extends activity { final int index = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); final textview tv = (textview) findviewbyid(r.id.quote); resources res = getresources(); final mystring = res.getstringarray(r.array.quote); final string q = mystring[rgenerator.nextint(mystring.length)]; tv.settext(q); button btnnext = (button) findviewbyid(r.id.btnnext); button btnprevious = (button) findviewbyid(r.id.btnprevious); button btn = (button) findviewbyid(r.id.btn); btnnext.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { string q = mystring[index++]; tv.settext(q); } }); btnprevious.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { string q = mystring[index--]; tv.settext(q); } }); btn.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { mystring = getresources().getstringarray(r.array.quote); index = rgenerator.nextint(mystring.length); string q = mystring[index]; tv.settext(q); } }); } }}
Comments
Post a Comment