3

In one class I got a string with facts in an array like this:

public class FactBook {
    public String[] mFacts = {"a", "b", "c"};
    ....

Now I got a random generator like this:

public String getFact() {
    String fact = "";

    Random randomGenerator = new Random();  

    int randomNumber = randomGenerator.nextInt(mFacts.length);

    fact = mFacts[randomNumber];

    return fact;
}

My randomButton in the activity class is like 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 the Label with our dynamic fact
        factLabel.setText(fact);

        int color = mColorWheel.getColor();
        relativeLayout.setBackgroundColor(color);
        showFactButton.setTextColor(color);
    }
};
showFactButton.setOnClickListener(listener);

Now I would like to create a button that goes through the array step by step (like a nextButton and previousButton). But I'm stuck. Does someone has any ideas?

beresfordt
  • 5,088
  • 10
  • 35
  • 43

1 Answers1

1

Create class that accepts string array and operate on it, implementation is very basic, treat as 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 I am not familiar with android, I see in your sources that button was created in another place ( possible by some gui tool ). On each click on button it will iterate over array until reaches end. To dynamically create button ( as read from docs ) you 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 as 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// );    
}
Robert Wadowski
  • 1,407
  • 1
  • 11
  • 14
  • Thnx! I will try this ASAP, and hope this is what I needed. – José Alberto Jul 06 '15 at 10:28
  • it is worth to add boolean hasNext method for example, reset, depends how do you want to use it ... – Robert Wadowski Jul 06 '15 at 10:29
  • Robert thank you for your fast reply. But when I try this, I´m not able to create my nextButton from the String next.....I´m edting the button I alrady have. (I´m still a newbie on this) – José Alberto Jul 06 '15 at 20:11
  • So you have to create these buttons in this studio, however if amount of string is variable you have to create them dynamically as well. I don't believe that . Here you have link http://stackoverflow.com/questions/1851633/how-to-add-button-dynamically-in-android so it is possible – Robert Wadowski Jul 06 '15 at 21:07
  • Ok, thnx alot for the fast replies!! I will try to adapt all and study more :) – José Alberto Jul 06 '15 at 21:15