Click here to Skip to main content
15,897,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making a random number generator app in this app we will take the random number range like 0-100 then when we click on the generate btn it will set the number on the button. Manually we can set that value of random number and set it on button.if i set the random number 4 will always correct it means the fourth button will always be correct. Now i want to make the shuffle of random number on button so i create the Random number Array and ArrayList of button so i add it on the ArrayList but i don't know how to set the value of random number array on arraylist of button.If there is an other way to solve this problem then guide me


What I have tried:

package com.example.quizoptioninarraylist;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    Button genbtn,optbtn1,optbtn2,optbtn3,optbtn4;
    EditText mintext,maxtext;
    Random rand[]=new Random[4];
    int minrange,maxrange;
    ArrayList <Button>shufflearray=new ArrayList<Button>();
    int random[]=new int[4];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        genbtn=(Button)findViewById(R.id.gen);

       
        shufflearray.add((Button)findViewById(R.id.opt1));
        shufflearray.add((Button)findViewById(R.id.opt2));
        shufflearray.add((Button)findViewById(R.id.opt3));
        shufflearray.add((Button)findViewById(R.id.opt4));
        mintext=(EditText)findViewById(R.id.min);
        maxtext=(EditText)findViewById(R.id.max);

        genbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                minrange=Integer.parseInt(mintext.getText().toString());
                maxrange=Integer.parseInt(maxtext.getText().toString());
                for(int i=0;i<rand.length;i++){
                    random[i]=rand[i].nextInt(maxrange-minrange)+maxrange;
                }
                for(int i=0;i<shufflearray.size();i++) {
                }
            }
        });

    }
}
Posted
Updated 30-Dec-19 22:04pm

1 solution

It is not absolutely clear what you are trying to do here, but your code looks overly complicated. All you really need is an array to hold 4 random values. Each time you generate new values you just set the text of the buttons to the new values. Then when the user clicks a button you can get its value from the text.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900