• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble figuring out how to randomize an array into text field

 
Jeff Nestor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the program as I have it thus far. It outputs a random number in one text field, but I need it to output a random array element in another field, both on the same button click. My array is saved as core.xml under the values section.

import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class Core extends ActionBarActivity {

Random rndNumbers = new Random();
int d20 = rndNumbers.nextInt(11) + 10;
Button random;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_core);
random = (Button)findViewById(R.id.btnNext);
display = (TextView)findViewById(R.id.txtNumber);

random.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
d20 = rndNumbers.nextInt(11) + 10;
display.setText("Workout Reps " + d20);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_core, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);


 
Jeff Nestor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been able to get it to work with:


final Random randomGenerator = new Random();
final ArrayList core = new ArrayList() {{ add("Crunch"); add("V-Ups"); add("Reverse Crunch"); add("Flutter Kicks"); add("Left Side Plank (in seconds)");add("Right Side Plank (in seconds)");
add("Oblique V-Ups");add("Russian Twist");add("Frind Plank (in seconds)");add("Bicycle Kicks");add("Toe Taps");add("Superman/Banana");add("Plank w/alternating Arm/Leg raises");add("Situps");
add("Leg Raises");add("Scissor Kicks");add("Windmills");add("Toe Touchers");

}};

random.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
d20 = rndNumbers.nextInt(11) + 10;
display.setText("Workout Reps " + d20);

String item = (String) core.get(randomGenerator.nextInt(core.size()));
((TextView) findViewById(R.id.txtWrkCore)).setText(item);
}
});
}

But that's putting the array into the activity, not pulling from a value string.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic