java - How to get next button in String Array -
in 1 class got string facts in array this:
public class factbook { public string[] mfacts = {"a", "b", "c"}; ....
now got random generator this:
public string getfact() { string fact = ""; random randomgenerator = new random(); int randomnumber = randomgenerator.nextint(mfacts.length); fact = mfacts[randomnumber]; return fact; }
my randombutton in activity class this:
final textview factlabel = (textview) findviewbyid(r.id.facttextview); final button showfactbutton = (button) findviewbyid(r.id.showfactbutton); final relativelayout relativelayout = (relativelayout) findviewbyid(r.id.relativelayout); final mediaplayer mmediaplayer = mediaplayer.create(getapplicationcontext(), r.raw.button); view.onclicklistener listener = new view.onclicklistener() { @override public void onclick(view v) { mmediaplayer.start(); string fact = mfactbook.getfact(); //update label our dynamic fact factlabel.settext(fact); int color = mcolorwheel.getcolor(); relativelayout.setbackgroundcolor(color); showfactbutton.settextcolor(color); } }; showfactbutton.setonclicklistener(listener);
now create button goes through array step step (like nextbutton , previousbutton). i'm stuck. has ideas?
create class accepts string array , operate on it, implementation basic, treat poc ;)
public class facts { private string[] mfacts; private int current = 0; public facts( string[] mfacts ) { this.mfacts = mfacts; } public boolean hasprevious() { return current >= 0; } public boolean hasnext() { return current < mfacts.length; } public string next() { if( !hasnext() ) { return null; } string v = mfacts[ current ]; current++; return v; } public string previous() { if( !hasprevious() ) { return null; } string v = mfacts[ current ]; current--; return v; } public string random() { random randomgenerator = new random(); int randomnumber = randomgenerator.nextint(mfacts.length); return mfacts[randomnumber]; } }
update somewhere in code facts facts = new facts( string[] array ); although not familiar android, see in sources button created in place ( possible gui tool ). on each click on button iterate on array until reaches end. dynamically create button ( read docs ) may use this.
public class buttoncreator { private facts facts; public buttoncreator( facts facts ) { this.facts = facts; } public boolean cancreatenextbutton() { return facts.hasnext(); } public boolean cancreatepreviousbutton() { return facts.hasprevious(); } public button createnextbutton( context context ) { button button = new button( context ); button.settext( facts.next() ); return button; } public button createpreviousbutton( context context ) { button button = new button( context ); button.settext( facts.previous() ); return button; } }
then after creation setup appropriate listener in pasted code
then during initialization of form
string[] arr = {"aaa","bbbb","ccc"}; facts f = new facts( array ); buttoncreator creator = new buttoncreator( f ); while( creator.cancreatenextbutton() ) { button button = creator.nextbutton( //view context here// ); button.setlistener( //listener here// ); }
Comments
Post a Comment