How can I generate different random numbers regularly in 4 second? - java

I'm trying to write a bingo game code. I want to produce 70 random numbers that change every 4 seconds.
I created a countdowntimer and created a random number in 4 seconds by defining a runnable into the finnish section in countdowntimer. But these random numbers may not be different from each other.
Handler handler;
Runnable run;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random random = new Random();
int i = random.nextInt(70);
Random random2 = new Random();
int z = random2.nextInt(70);
int y =random2.nextInt(70);
int x =random2.nextInt(70);
int w =random2.nextInt(70);
int q =random2.nextInt(70);
int v =random2.nextInt(70);
int m =random2.nextInt(70);
int n =random2.nextInt(70);
int l =random2.nextInt(70);
final TextView textView3 = (TextView) findViewById(R.id.textView3);
final TextView textView4 = (TextView) findViewById(R.id.textView4);
final TextView textView5 = (TextView) findViewById(R.id.textView5);
final TextView textView6 = (TextView) findViewById(R.id.textView6);
final TextView textView7 = (TextView) findViewById(R.id.textView7);
final TextView textView8 = (TextView) findViewById(R.id.textView8);
final TextView textView9 = (TextView) findViewById(R.id.textView9);
final TextView textView10 = (TextView) findViewById(R.id.textView10);
final TextView textView2 = (TextView) findViewById(R.id.textView2);
textView2.setText("Lucky number: " + i);
final int a = Integer.parseInt(textView3.getText().toString());
final int b = Integer.parseInt(textView4.getText().toString());
final int c = Integer.parseInt(textView5.getText().toString());
final int d = Integer.parseInt(textView6.getText().toString());
final int e = Integer.parseInt(textView7.getText().toString());
final int f = Integer.parseInt(textView8.getText().toString());
final int g = Integer.parseInt(textView9.getText().toString());
final int h = Integer.parseInt(textView10.getText().toString());
textView9.setText("" + z);
textView10.setText(" "+ y);
textView8.setText(" "+ x);
textView7.setText(" "+ w);
textView6.setText(" "+ q);
textView5.setText(" "+ l);
textView4.setText(" "+ m);
textView3.setText(" "+ n);
CountDownTimer ct0 = new CountDownTimer(60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Remaining time: " + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
}
}.start();
CountDownTimer ct1 = new CountDownTimer(4000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
handler = new Handler();
run = new Runnable() {
#Override
public void run() {
int[] numbers = new int[70];
Random random = new Random();
int i = random.nextInt(70);
TextView textView2 = (TextView) findViewById(R.id.textView2);
textView2.setText("Lucky number: " + i);
if ( i == a) {
textView3.setText("ok");
} else if (i == b) {
textView4.setText("ok");
} else if (i == c) {
textView5.setText("ok");
} else if (i == d) {
textView6.setText("ok");
} else if (i == e) {
textView7.setText("ok");
} else if(i == f) {
textView8.setText("ok");
} else if (i == g) {
textView9.setText("ok");
} else if (i == h) {
textView10.setText("ok");
}
handler.postDelayed(this, 4000);
}
};
handler.post(run);
}
}.
start();
}
For 60 seconds, I want to create 70 different numbers every 4 seconds and match these numbers to the other 8 variables. I'd appreciate it if you could help. thanks.

I can give to you a way to do this, to avoid what you are saying :
But these random numbers may not be different from each other.
That's obvious because you do not control it.
I'd say to create a a Set<Integer> of your size that is 70, so you can fill this array doing a simple for loop.
for (int i = 0; i<70; i++) yourSetList.add(i);
Then a good way to "simulate" the bingo is shuffling the list, so you can use
Collections shuffle
Then you can generate a random like this :
Random random = new Random();
int random = random.nextInt(yourSetList.size()) + 1;
Then use random as an index
yourSetList.get(random);
Make sure you remove it from the list to avoid your initial problem
yourSetList.remove(random);

Related

How to randomly generate random operators in android studio

public class MainActivity extends AppCompatActivity {
Button startButton;
ArrayList<Integer> answers=new ArrayList<Integer>();
int LocationOfRightAnswer;
TextView resultTextView;
TextView pointText;
Button button1;
Button button2;
Button button3;
Button button4;
int score=0;
int numberofquestions = 0;
TextView sumText;
TextView timertext;
RelativeLayout game;
Button playAgain;
public void playAgain(View view) {
score = 0;
numberofquestions = 0 ;
timertext.setText("30s");
pointText.setText("0/0");
resultTextView.setText("");
playAgain.setVisibility(View.INVISIBLE);
generateQuestion();
new CountDownTimer(30100,1000) {
#Override
public void onTick(long millisUntilFinished) {
timertext.setText(String.valueOf(millisUntilFinished / 1000) + "s");
}
#Override
public void onFinish() {
playAgain.setVisibility(View.VISIBLE);
timertext.setText("0s");
resultTextView.setText("Your Score is " + Integer.toString(score) + "/" + Integer.toString(numberofquestions));
}
}.start();
}
public void generateQuestion() {
Random random = new Random();
int a = random.nextInt(21);
int b = random.nextInt(21);
char[] ops = {'+' , '-' ,'*', '/'};
int l = random.nextInt(3) ;
sumText.setText(Integer.toString(a) + "+" + Integer.toString(b) );
LocationOfRightAnswer = random.nextInt(4);
answers.clear();
int incorrectAnswer;
for (int i=0; i<4; i++) {
if (i == LocationOfRightAnswer) {
answers.add(a + b);
}else {
incorrectAnswer = random.nextInt(41);
while (incorrectAnswer == a + b) {
incorrectAnswer = random.nextInt(41);
}
answers.add(incorrectAnswer);
}
}
button1.setText(Integer.toString(answers.get(0)));
button2.setText(Integer.toString(answers.get(1)));
button3.setText(Integer.toString(answers.get(2)));
button4.setText(Integer.toString(answers.get(3)));
}
public void chooseAnswer(View view) {
if (view.getTag().toString().equals(Integer.toString(LocationOfRightAnswer))) {
score++;
resultTextView.setText("Correct");
} else {
resultTextView.setText("Wrong!");
}
numberofquestions++;
pointText.setText(Integer.toString(score) + "/" + Integer.toString(numberofquestions));
generateQuestion();
}
public void start (View view) {
startButton.setVisibility(View.INVISIBLE);
game.setVisibility(View.VISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton =(Button) findViewById(R.id.startButton);
sumText = (TextView) findViewById(R.id.sumTextView);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
resultTextView = (TextView) findViewById(R.id.resultTextView);
pointText = (TextView) findViewById(R.id.pointsTextView);
timertext = (TextView) findViewById(R.id.timerTextView);
playAgain = (Button) findViewById(R.id.playAgain);
game = (RelativeLayout) findViewById(R.id.gamelayout) ;
playAgain(findViewById(R.id.playAgain));
}
}
please help me to adjust my code so that I the app can use all operators instead of just addition. I have tried to do it but I have only managed to create the array list for the operators but I could not find a way to change the code to include all of the operators.
Try this:
ArrayList<Double> answers=new ArrayList<Double>();
public void generateQuestion() {
Random random = new Random();
int a = random.nextInt(21);
int b = random.nextInt(21);
char[] ops = {'+' , '-' ,'*', '/'};
int l = random.nextInt(3) ;
char op = ops[random.nextInt(4)];
double correctAns=0;
switch(op){
case '+' : correctAns = a+b;
break;
case '/' : correctAns = (double)a/b;
break;
case '*' : correctAns = a*b;
break;
case '-' : correctAns = a-b;
break;
}
sumText.setText(Integer.toString(a) + op + Integer.toString(b) );
LocationOfRightAnswer = random.nextInt(4);
answers.clear();
int incorrectAnswer;
for (int i=0; i<4; i++) {
if (i == LocationOfRightAnswer) {
answers.add(correctAns);
}else {
incorrectAnswer = random.nextInt(41);
while (incorrectAnswer != correctAns) {
incorrectAnswer = random.nextInt(41);
}
answers.add((double)incorrectAnswer);
}
}
button1.setText(Double.toString(answers.get(0)));
button2.setText(Double.toString(answers.get(1)));
button3.setText(Double.toString(answers.get(2)));
button4.setText(Double.toString(answers.get(3)));
}
change ArrayList<Integer> answers=new ArrayList<Integer>(); to ArrayList<Double> answers=new ArrayList<Double>(); (int/int can be a double)
create a double variable contains the right answer (int/int can be a double)
choose an operation randomly.
use switch to define what operation is needed on a and bin case that every operation selected.
set the text to Integer.toString(a) + op + Integer.toString(b) instead of Integer.toString(a) + "+" + Integer.toString(b)
swap a+b to correctAns

Random number groups

I am making app where shows me on display random set of numbers. And here is my question becouse my code looks like that:
tv1 = (TextView) findViewById(R.id.textView);
btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final java.util.Random rand = java.util.concurrent.ThreadLocalRandom.current();
new java.util.Timer().scheduleAtFixedRate(new java.util.TimerTask() {
#Override
public void run() {
tv1.setText(rand.nextInt(6) + "-" + rand.nextInt(6) + "-" + rand.nextInt(6));
}
}, 0, 3000);
}
});
}
}
Is it possible that the application instead of numbers from 6 to 0 would take ready sets of numbers that I create?
Implement the method given below inside your activity class and you can use it to achieve your requirements:
// To get the numbers from 0 to 6 use upperBound as 7.
private static String getResult(int lowerBound, int upperBound){
final ThreadLocalRandom rand = ThreadLocalRandom.current();
List<Integer> list = new ArrayList<Integer>();
int number;
for(int counter = 0; counter < 3;counter++){
number = rand.nextInt(lowerBound, upperBound);
while(list.contains(number)) {
number = rand.nextInt(lowerBound, upperBound);
}
list.add(number);
}
Collections.sort(list); //Sorts the list
return list.get(0) + "-" + list.get(1) + "-" + list.get(2);
}
In your TextView tv1 inside run() method; simply call this method, something like:
tv1.setText(getResult(0, 7)); // Remember in the sequence; 0 is inclusive and 7 is exclusive.
You can see the sample run here.
If you want to draw random numbers from a List, you can use rand.nextInt(list.size()) to get a random index to draw a number from, as the upper bound argument for nextInt is exclusive.
final java.util.List<Integer> numbers = java.util.Arrays.asList(1, 4, 534, 94);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final java.util.Random rand = java.util.concurrent.ThreadLocalRandom.current();
new java.util.Timer().scheduleAtFixedRate(new java.util.TimerTask() {
#Override
public void run() {
tv1.setText(numbers.get(rand.nextInt(numbers.size())) + "-" + numbers.get(rand.nextInt(numbers.size())) + "-" + numbers.get(rand.nextInt(numbers.size()));
}
}, 0, 3000);
}
});
}
}

How to make my variable = the item from the Drop Down Menu

I have a Spinner that has 16 values. 24, 24 1/16, 24 2/16 .... 25. I want to take the one the selected and put it into a variable "w" in the form of a double(decimal). I want to use that variable in the calculations below. I've only made three if Statements for simplicity, but just know there will be 16 of them if it affects it somehow. But in the Calculations they show up as not defined.
public void calculate(View view) {
EditText length_find = findViewById(R.id.feet);
EditText pounds_find = findViewById(R.id.pounds);
DecimalFormat df = new DecimalFormat("###.###");
Spinner width_select = findViewById(R.id.width_select);
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.width));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
width_select.setAdapter(myAdapter);
width_select.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
if (position == 0){
double w = 24;
}
if (position == 1){
double w = 24.0625;
}
if (position == 2){
double w = 24.125;
}
}
});
try {
double d = .29;
double h = .002;
//This Calculates if Pounds are given
if (length_find.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Given Pounds", LENGTH_LONG).show();
double pounds = Double.parseDouble(pounds_find.getText().toString());
//THIS IS THE MATH PORTION
double length_d = (pounds / (w * h * d * 12));
String length = Double.toString(Double.parseDouble(df.format(length_d)));
final TextView zTextView = (TextView) findViewById(R.id.length_show);
zTextView.setText("Feet: " + length);
final TextView mTextView = (TextView) findViewById(R.id.pound_show);
mTextView.setText("Pounds: ");
length_find.getText().clear();
pounds_find.getText().clear();
}
//This Calculates if Length is given
if (pounds_find.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Given Length", LENGTH_LONG).show();
Double length = Double.parseDouble(length_find.getText().toString());
//THIS IS THE MATH PORTION
double answer_pounds_d = (w * h * length * 12 * d);
String answer_pounds = Double.toString(Double.parseDouble(df.format(answer_pounds_d)));
final TextView mTextView = (TextView) findViewById(R.id.pound_show);
mTextView.setText("Pounds: " + answer_pounds);
final TextView zTextView = (TextView) findViewById(R.id.length_show);
zTextView.setText("Feet: ");
length_find.getText().clear();
pounds_find.getText().clear();
}
if((pounds_find).getText().toString().trim().length() > 0 && (length_find).getText().toString().trim().length() > 0){
Toast.makeText(MainActivity.this, "Whata hell you need me for mate!", LENGTH_LONG).show();
}
}
catch(Exception ex) {
//Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
Since you're only creating the variable in the if statements inside onItemClick, they're only accessible inside those if statements. My suggestion would probably be to create it right before the if statements and then assign it a value in the statements. And in order to use it you'll probably have to move all the math and stuff to inside onItemClick.
Edit: untested, but my guess would be something like this:
public void calculate(View view) {
EditText length_find = findViewById(R.id.feet);
EditText pounds_find = findViewById(R.id.pounds);
DecimalFormat df = new DecimalFormat("###.###");
Spinner width_select = findViewById(R.id.width_select);
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.width));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
width_select.setAdapter(myAdapter);
width_select.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
double w = 0;
if (position == 0){
double w = 24;
}
if (position == 1){
double w = 24.0625;
}
if (position == 2){
double w = 24.125;
}
try {
double d = .29;
double h = .002;
//This Calculates if Pounds are given
if (length_find.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Given Pounds", LENGTH_LONG).show();
double pounds = Double.parseDouble(pounds_find.getText().toString());
//THIS IS THE MATH PORTION
double length_d = (pounds / (w * h * d * 12));
String length = Double.toString(Double.parseDouble(df.format(length_d)));
final TextView zTextView = (TextView) findViewById(R.id.length_show);
zTextView.setText("Feet: " + length);
final TextView mTextView = (TextView) findViewById(R.id.pound_show);
mTextView.setText("Pounds: ");
length_find.getText().clear();
pounds_find.getText().clear();
}
//This Calculates if Length is given
if (pounds_find.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Given Length", LENGTH_LONG).show();
Double length = Double.parseDouble(length_find.getText().toString());
//THIS IS THE MATH PORTION
double answer_pounds_d = (w * h * length * 12 * d);
String answer_pounds = Double.toString(Double.parseDouble(df.format(answer_pounds_d)));
final TextView mTextView = (TextView) findViewById(R.id.pound_show);
mTextView.setText("Pounds: " + answer_pounds);
final TextView zTextView = (TextView) findViewById(R.id.length_show);
zTextView.setText("Feet: ");
length_find.getText().clear();
pounds_find.getText().clear();
}
if((pounds_find).getText().toString().trim().length() > 0 && (length_find).getText().toString().trim().length() > 0){
Toast.makeText(MainActivity.this, "Whata hell you need me for mate!", LENGTH_LONG).show();
}
}
catch(Exception ex) {
//Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
}
});
Just make your variable 'w' a global variable for the activity.As your variable 'w' is local to the scope of 'if statements' and is undefined outside it.

How can I check if all radiogroups have a selected answer when populated by a recyclerView?

I've only been writing code for a couple months and I'm still fairly newbie to a lot of techniques, but I'm working on a math training quiz for my 5 year old son. I've been trying to a few different "games" to play and challenge himself. I finished a time-trial yesterday, and a pattern match last week but now I'm stuck on the current game, which is a levels challenge. The player must answer 10 questions correctly to move to the next level (where the questions get harder).
I'm using a RecyclerView to create the questions. Each question is populated with random integers, and possible answers are selectable as RadioButtons.
So far I've been able to figure out how to add the formatting and update the ViewHolder, but now I'm stuck with trying to create a method in my Fragment to check for all questions answered. I believe that has to be done in the Fragment, since the Fragment is where I define how many questions to create and where the random numbers are generated and passed into the ArrayList that the Recycler Adapter uses to update the View Holder.
I hope that I'm explaining myself well enough, so my question is how can I check that all radiogroups meet the following parameters:
Has a selected radioButton
Is answered correctly (if they're answered incorrectly, they won't move to the next level, but instead will be given a new set of the same difficulty questions.
Here is my Fragment Class:
public class FragmentLevelChallenge extends Fragment{
public static final String TAG = "LevelChallengeFragment";
RecyclerView recyclerView;
RVAdapterMulti adapterMulti;
private List<multiquestion> QuestionsMulti;
int firstDig;
int secondDig;
int correctAnswer;
int incorrectOne;
int incorrectTwo;
int incorrectThree;
int thisPosition;
int selectedAnswers;
int questionsAnswered;
//levelChallenge defines the current challenge level
int levelChallenge;
int maxBound = 10;
int minBound = 1;
int adjustBound = 1;
RadioGroup radioGroupRecycler;
public static FragmentLevelChallenge newInstance() {
FragmentLevelChallenge fragment = new FragmentLevelChallenge();
return fragment;
}
#Override
public void onCreate (Bundle savedInstanceState) {super.onCreate(savedInstanceState);}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_level_challenge, container, false);
levelChallenge = 1;
QuestionsMulti = new ArrayList<>();
recyclerView = view.findViewById(R.id.recycler_view_display_multi);
radioGroupRecycler = view.findViewById(R.id.radio_group_multi);
LinearLayoutManager layoutManager = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(layoutManager);
TextView levelsText = view.findViewById(R.id.level_challenge);
levelsText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
initializeRecyclerAdapter();
createQuestionBatch();
return view;
}
private void createQuestionBatch() {
//Integer values to define the minimum, maximum and adjustment for random integers
if (selectedAnswers == 5) {
levelChallenge++;
}
//Checks the current level to determine the boundaries for difficulty of random integers
switch (levelChallenge) {
case 1: //If level 1 (bounds 10,1,1)
maxBound = 10;
minBound = 1;
adjustBound = 1;
break;
case 2: //If level 2 (bounds 20,1,4)
maxBound = 20;
minBound = 1;
adjustBound = 4;
break;
case 3: //If level 3 (bounds 25,10,5)
maxBound = 25;
minBound = 10;
adjustBound = 5;
break;
case 4: //If level 4 (bounds 30,10,7)
maxBound = 30;
minBound = 10;
adjustBound = 7;
break;
case 5: //If level 5 (bounds 50,10,9)
maxBound = 50;
minBound = 10;
adjustBound = 9;
break;
case 6: //If level 6 (bounds 75,10,15)
maxBound = 75;
minBound = 10;
adjustBound = 15;
break;
case 7: //If level 7 (bounds 100,10,18)
maxBound = 100;
minBound = 10;
adjustBound = 18;
break;
//If level 8 (bounds random integers?)
}
//Creates the random integers for math functions
int questionsToFillRecycler = 5;
for (int i = 0; i < questionsToFillRecycler; i++) {
final Random r = new Random();
Random position = new Random();
thisPosition = position.nextInt(4 - 1) + 1;
//Creates the array list for incorrect answers
firstDig = r.nextInt(maxBound - minBound) + adjustBound;
secondDig = r.nextInt(maxBound - minBound) + adjustBound;
if (firstDig < secondDig) {
correctAnswer = secondDig - firstDig;
incorrectOne = secondDig - firstDig - 1;
incorrectTwo = secondDig - firstDig + 2;
incorrectThree = (secondDig + firstDig) - 2;
} else {
correctAnswer = firstDig + secondDig;
incorrectOne = secondDig + firstDig - 2;
incorrectTwo = secondDig + firstDig + 1;
incorrectThree = (firstDig - secondDig) + 3;
}
//Convert integer to string values
String firstNumber = String.valueOf(firstDig);
String secondNumber = String.valueOf(secondDig);
String firstIncorrect = String.valueOf(incorrectOne);
String secondIncorrect = String.valueOf(incorrectTwo);
String thirdIncorrect = String.valueOf(incorrectThree);
String correct = String.valueOf(correctAnswer);
List<String> answers = new Vector<>();
answers.add(correct);
answers.add(firstIncorrect);
answers.add(secondIncorrect);
answers.add(thirdIncorrect);
Collections.shuffle(answers);
if (firstDig < secondDig) {
//Checks if 1st integer is less than 2nd integer
QuestionsMulti.add(new multiquestion(secondNumber, firstNumber, answers.get(0), "-", answers.get(1), answers.get(2), answers.get(3)));
} else if (firstDig > secondDig) {
//Checks if 1st integer is greater than 2nd integer
QuestionsMulti.add(new multiquestion(secondNumber, firstNumber, answers.get(0), "+", answers.get(1), answers.get(2), answers.get(3)));
} else {
//Checks if 1st integer is greater than 2nd integer
QuestionsMulti.add(new multiquestion(secondNumber, firstNumber, answers.get(0), "+", answers.get(1), answers.get(2), answers.get(3)));
}
//Updates the RecyclerView Adapter
adapterMulti.notifyDataSetChanged();
}
}
private void initializeRecyclerAdapter() {
adapterMulti = new RVAdapterMulti(QuestionsMulti);
recyclerView.setAdapter(adapterMulti);
}
}
And here is my RecyclerAdapter:
public class RVAdapterMulti extends RecyclerView.Adapter<RVAdapterMulti.ViewHolder> {
private static String TAG = "RadioMultiAdapter";
private LayoutInflater inflater;
private List<multiquestion> QuestionsMulti;
private int questionsAnswered;
private AdapterView.OnItemSelectedListener onSelect;
public RVAdapterMulti(List<multiquestion> QuestionMulti) {
this.QuestionsMulti = QuestionMulti;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_multi, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
multiquestion currentQuestion = QuestionsMulti.get(position);
holder.setFirstDigit(currentQuestion.firstNumber);
holder.setSecondDigit(currentQuestion.secondNumber);
holder.setMathFunction(currentQuestion.mathFunc);
holder.setOptions(currentQuestion, position);
}
#Override
public int getItemCount() {
if (QuestionsMulti == null) {
return 0;
} else {
return QuestionsMulti.size();
}
}
class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayoutContainer;
private TextView firstDigitView;
private TextView secondDigitView;
private TextView mathFunctionView;
private TextView mathAnswerView;
private RadioGroup radioGroupOptions;
private RadioButton radioButtonOne, radioButtonTwo;
private RadioButton radioButtonThree, radioButtonFour;
public ViewHolder(View itemView) {
super(itemView);
firstDigitView = itemView.findViewById(R.id.first_digit);
secondDigitView = itemView.findViewById(R.id.second_digit);
mathFunctionView = itemView.findViewById(R.id.math_function);
mathAnswerView = itemView.findViewById(R.id.math_answer);
radioGroupOptions = itemView.findViewById(R.id.radio_group_multi);
radioButtonOne = itemView.findViewById(R.id.radio_button_one);
radioButtonTwo = itemView.findViewById(R.id.radio_button_two);
radioButtonThree = itemView.findViewById(R.id.radio_button_three);
radioButtonFour = itemView.findViewById(R.id.radio_button_four);
}
public void setFirstDigit(String firstDigit) {
firstDigitView.setText(firstDigit);
}
public void setSecondDigit(String secondDigit) {
secondDigitView.setText(secondDigit);
}
public void setMathFunction(String mathFunction) {
mathFunctionView.setText(mathFunction);
}
public void setOptions(final multiquestion question, int position) {
radioGroupOptions.setTag(position);
radioButtonOne.setText(question.correct);
radioButtonTwo.setText(question.incorrectOne);
radioButtonThree.setText(question.incorrectTwo);
radioButtonFour.setText(question.incorrectThree);
if (question.mathFunc.equals("+")) {
question.correctSolution = Integer.parseInt(question.firstNumber) + Integer.parseInt(question.secondNumber);
} else {
question.correctSolution = Integer.parseInt(question.firstNumber) - Integer.parseInt(question.secondNumber);
}
if(question.isAnswered) {
radioGroupOptions.check(question.checkedId);
} else {
radioGroupOptions.check(-1);
}
radioGroupOptions.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int pos = (int) group.getTag();
multiquestion que = QuestionsMulti.get(pos);
que.isAnswered = true;
que.checkedId = checkedId;
radioGroupOptions.setClickable(false);
radioButtonOne.setClickable(false);
radioButtonTwo.setClickable(false);
radioButtonThree.setClickable(false);
radioButtonFour.setClickable(false);
questionsAnswered = questionsAnswered + 1;
mathAnswerView.setText(String.valueOf(question.correctSolution));
String radioOneText = String.valueOf(radioButtonOne.getText());
String radioTwoText = String.valueOf(radioButtonTwo.getText());
String radioThreeText = String.valueOf(radioButtonThree.getText());
String radioFourText = String.valueOf(radioButtonFour.getText());
switch (que.checkedId) {
case R.id.radio_button_one:
if(radioOneText.equals(String.valueOf(question.correctSolution))) {
mathAnswerView.setTextColor(group.getResources().getColor(R.color.colorCorrect));
} else {
mathAnswerView.setTextColor(group.getResources().getColor(R.color.colorIncorrect));
}
break;
case R.id.radio_button_two:
if(radioTwoText.equals(String.valueOf(question.correctSolution))) {
mathAnswerView.setTextColor(group.getResources().getColor(R.color.colorCorrect));
} else {
mathAnswerView.setTextColor(group.getResources().getColor(R.color.colorIncorrect));
}
break;
case R.id.radio_button_three:
if(radioThreeText.equals(String.valueOf(question.correctSolution))) {
mathAnswerView.setTextColor(group.getResources().getColor(R.color.colorCorrect));
} else {
mathAnswerView.setTextColor(group.getResources().getColor(R.color.colorIncorrect));
}
break;
case R.id.radio_button_four:
if(radioFourText.equals(String.valueOf(question.correctSolution))) {
mathAnswerView.setTextColor(group.getResources().getColor(R.color.colorCorrect));
} else {
mathAnswerView.setTextColor(group.getResources().getColor(R.color.colorIncorrect));
}
break;
}
}
});
}
}
}
Any help would be greatly appreciated.
The easiest way will be to keep track of correct answers in your QuestionMulti list (you should refactor that to camel case by convention). Add an attribute to your multiquestion (should be MultiQuestion) class called answeredCorrectly with a default value of false.
Then every time onCheckChanged is called, set the value at that position in the list and also loop through the list to check if every answer is correct.

Not able to display very large calculation value in editext

private TextView info;
private EditText input;
private Button getInfo;
long answer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInfo = (Button) findViewById(R.id.button1);
getInfo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
input = (EditText) findViewById(R.id.editText1);
String s2 = input.getText().toString();
long inputNumber = Integer.parseInt(s2);
for (int i = 0; i <= inputNumber; i++) {
answer = fibonacci(i);
}
input.setText(answer + "");
}
});
}
public static long fibonacci(long n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
I made a program which generates fibonacci series for the number I give input through edit text and display the output value in edittext .Issue is when I enter 30 it generates fibonacci series but when I enter 50 or 100 app does not respond and stops .
I think it is because you have a lot of calculation on UI thread.
Try calculate it in AssyncTask, for example.

Categories