-4

I have created a code for random numbers to show each number in editText. Im now trying to populate each number to an array and pass it to the next activity. When I do so, my app does not compile. Here is what I have so far: Your assistance will be appreciated.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtNext = findViewById(R.id.editTxtNext);
    next = findViewById(R.id.btnNext);
    bingo = findViewById(R.id.btnBingo);
    exit = findViewById(R.id.btnExit);
    List<Integer> myNumbers = new ArrayList<>();
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random numRandom = new Random();
                int val = numRandom.nextInt(99);
                txtNext.setText(Integer.toString(val));
            while (true) {
                if (!myNumbers.contains(val)) {
                    myNumbers.add(val);
                }
            }

        }

    });
    bingo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            value = myNumbers.toString();
            Intent intent = new Intent(MainActivity.this, MainActivity2.class);
            startActivity(intent);
        }
    });

Im trying to have an array from each generated value to show in a TextView of a different activity.

Thanks in advance Java Android studio

1 Answers1

1

There are a few issues in your code that need to be addressed. I'll provide you with a corrected version of your code along with explanations for the changes:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    txtNext = findViewById(R.id.editTxtNext);
    next = findViewById(R.id.btnNext);
    bingo = findViewById(R.id.btnBingo);
    exit = findViewById(R.id.btnExit);

    List<Integer> myNumbers = new ArrayList<>(); // Moved myNumbers declaration outside the click listener

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random numRandom = new Random();
            int val = numRandom.nextInt(99);
            txtNext.setText(Integer.toString(val));
            
            if (!myNumbers.contains(val)) {
                myNumbers.add(val);
            }
        }
    });

    bingo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, MainActivity2.class);
            intent.putIntegerArrayListExtra("numbersArray", (ArrayList<Integer>) myNumbers);
            startActivity(intent);
        }
    });
}

Here's what I've done:

  1. Moved the declaration of myNumbers outside of the onClick listener. This way, the arraylist will be accessible across the methods within the onCreate method.

  2. Removed the infinite loop inside the onClick of the "Next" button. This loop is unnecessary and will cause your app to hang.

  3. Changed the value assignment in the "Bingo" button's onClick listener to store the arraylist as an extra in the Intent. You can't directly convert a list to a string using toString().

  4. In the Intent, I'm using the putIntegerArrayListExtra method to pass the arraylist to the second activity.

Now, in your second activity (presumably MainActivity2), you can retrieve the arraylist like this:

List<Integer> receivedNumbers = getIntent().getIntegerArrayListExtra("numbersArray");

I tested it.

Sarah
  • 175
  • 4