0

I have an activity with 3 edittexts and a button. I have a second activity with 2 textviews.

When button is clicked i want two random edittexts values from activity1 to replace the text on textviews on activity2.

I managed to do that, but not randomly. How can I make it random?

Here is the first activity.

final EditText et  = (EditText) findViewById(R.id.editText);
        final EditText et1 = (EditText) findViewById(R.id.editText2);
        final EditText et2 = (EditText) findViewById(R.id.editText3);
        Button btn = (Button) findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick (View v){
                Intent i = new Intent(MainActivity.this, postavke.class);
                i.putExtra("prvi", et.getText().toString());
                i.putExtra("drugi", et1.getText().toString());
                i.putExtra("treci", et2.getText().toString());
                startActivity(i);
            }
        });
    }

Here is the second activity.

   TextView tv = (TextView) findViewById(R.id.asdf);
    tv.setText(getIntent().getExtras().getString("prvi"));

TextView dr = (TextView) findViewById(R.id.asdg);
dr.setText(getIntent().getExtras().getString("drugi"));
3
  • what do you mean by randomly ? Commented Sep 28, 2015 at 10:01
  • I think he has 2 slots, 3 values and wants to use 2 of the 3 values, randomly selected Commented Sep 28, 2015 at 10:01
  • @Tomo Can both the TextView contain same values? Commented Sep 28, 2015 at 10:08

5 Answers 5

2

In your second activity:

String[] texts = new String[]{
    getIntent().getExtras().getString("prvi"),
    getIntent().getExtras().getString("drugi"),
    getIntent().getExtras().getString("treci"),
};

TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);

Random random = new Random();
tv.setText(texts[random.nextInt(3)]);
dr.setText(texts[random.nextInt(3)]);

Or, for unique values:

First activity:

final EditText et  = (EditText) findViewById(R.id.editText);
    final EditText et1 = (EditText) findViewById(R.id.editText2);
    final EditText et2 = (EditText) findViewById(R.id.editText3);
    Button btn = (Button) findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick (View v){

            ArrayList<String> texts = new ArrayList<>();
            texts.add(et.getText().toString());
            texts.add(et1.getText().toString());
            texts.add(et2.getText().toString());

            Intent i = new Intent(MainActivity.this, postavke.class);
            i.putExtra("texts", texts);
            startActivity(i);
        }
    });
}

Second activity:

ArrayList<String> texts = getIntent().getExtras().getStringArrayList("texts");

TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);

Collections.shuffle(texts);
tv.setText(texts.get(0));
dr.setText(texts.get(1));
Sign up to request clarification or add additional context in comments.

6 Comments

it is possible that random number generate same number . it is better that just create a random number and delete it s index from array like this: texts.remove[random.nextInt(3)]]; and then use other texts array elements.
This should work, but there is a 33% chance the strings will be the same
Just posted the simplest answer! There was no mention of wanting them to be unique
@JoeFrostick Thanks for the code, yesterday it was fine, but today i realised Tim Castelijns was right, I do need them to be unique. Could you please edit the code so that one string doesn't show twice on the screen in the same time? Sorry that I didn't pay attention earlier.
I've updated the answer. The new answer also shows how you could send the array list as a while, reducing the number of argument values you need to remember
|
0

Add your Strings(Eg : getIntent().getExtras().getString("prvi")) from previous Activity to a ArrayList.

Then do simply Collection.shuffle(your_list);

Get items from ArrayList & set it to your TextViews.

Comments

0
  1. Declare an array that will contain the values:

public String[] randomAnswers = new String[3]; //3 because you said you have 3 random strings

  1. In your onCreate() add the values to the array: randomAnswers[0] = getIntent().getExtras().getString("prvi"); randomAnswers[1] = getIntent().getExtras().getString("drugi"); randomAnswers[2] = getIntent().getExtras().getString("treci");

  2. Select two random values:

private static final Random RANDOM = new Random(); //in java.util package

Inside onCreate:

final int randomInt1 = RANDOM.nextInt(3); //nextInt(int) goes from 0 to int-1, considering arrays start at index=0 we want to get a random number from: 0, 1, 2
int randomInt2;
do {
        randomInt2 = RANDOM.nextInt(3);
} while (randomInt1 == randomInt2);
final String firstRandomString = values[randomInt1];
final String secondRandomString = values[randomInt2];`

Comments

0

you can use some thing like this the edited form of @Joe Frostick answere:

String[] texts = new String[]{
getIntent().getExtras().getString("prvi"),
getIntent().getExtras().getString("drugi"),
getIntent().getExtras().getString("treci"),
                             };

TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);

Random random = new Random();
texts.remove(random.nextInt(3));
tv.setText(texts[0]);
dr.setText(texts[1]); 

Comments

0

Thanks for the help everyone, I really didn't expect so many answers in not much time. You're the best!

1 Comment

That's the point of StackOverflow. All fo us also post questions one day or another. And trying to answer questions is a good way to learn new features ;) hava a ncie day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.