How to get next button in String Array

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 activity class looks 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 nextButton and previousButton). But I'm stuck. Does anyone have any ideas?

+3


source to share


1 answer


Create a class that takes a string array and operates on it, the implementation is very simple, think of it 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 the code Facts Facts = new facts (String [] array); Although I'm not familiar with android, I see in your sources that the button was created elsewhere (probably with some gui tool). Each time the button is pressed, it will iterate through the array to the end. To create a button dynamically (as read from the docs) you can 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, configure the appropriate listener as in the inserted code

Then when initializing the 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// );    
}

      

+1


source







All Articles