My code is this:
public class startgame extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level1);
final Random rgenerator = new Random();
//setup the questions
List<String> questions1 = new ArrayList<String>();
questions1.add("Who is the actual CEO at Apple?");
questions1.add("Who is the actual CEO at Microsoft?");
questions1.add("Android is made by:");
String thequestion = questions1.get(rgenerator.nextInt(questions1.size()));
TextView question = (TextView)findViewById(R.id.textView1);
question.setText(thequestion);
questions1.remove(thequestion);
//Initialise the button variables
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
if (thequestion.equals("Who is the actual CEO at Apple?")) {
List<String> questions1res = new ArrayList<String>();
questions1res.add("Steve Jobs");
questions1res.add("Steven Sinofsky");
questions1res.add("Tim Cook");
questions1res.add("Steve Ballmer");
button1.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button1.getText());
button2.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button2.getText());
button3.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button3.getText());
button4.setText(questions1res.get(rgenerator.nextInt(questions1res.size())));
questions1res.remove(button4.getText());
}
}
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
case R.id.button2:
case R.id.button3:
case R.id.button4:
}
}
}
What id does is this:
Choose 1 question from that arraylist of questions. Create the buttons, and put the chosen question in a string, and show that string on the screen. If that string is 'Who is the actual CEO at Apple?' then randomly put Steve Jobs and all those answers on buttons.
What I want is this:
If the user presses the button that contains: 'Tim Cook' then:
Remove 'Who is the actual CEO at Apple?' from the questions list, and randomly chose another question from the ArrayList of questions, and randomly put the answers on the buttons (the same stuff I already did, just that is another question).
My problem is that I can't really have acces to the array to delete it,because all I got is the case when the button is pressed.I tried to make a function,but every time I execute the function,the list is always recreated....
Can someone correct the code for me? And add what is missing?
Put the code that displays a random question in a new method (let's call it displayNewQuestion()) and let questions1 be a field of your Activity class. displayNewQuestion will then be able to use the activity-wide question array, and the click handler can remove a question out of it.
Try to change the scope of the ArrayList (use a private member for example) to enable access from your onClick method… I assume you'll have to do the same with your adapter to tweak it to your needs.
Update:
A quick-and-dirty implementation (without adapter nor ViewHolder, etc.):
package com.stackoverflow.randomarray;
import java.lang.String;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SoRandomArray extends Activity implements View.OnClickListener {
private Random mRandom = new Random();
private List<String> mQuestionsList;
private String mCurrentQuestion = null;
private List<String> mAnswersList;
TextView mQuestionTv;
Button mButton1;
Button mButton2;
Button mButton3;
Button mButton4;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mQuestionsList = new ArrayList<String>();
mAnswersList = new ArrayList<String>();
initQuizData();
mQuestionTv = (TextView)findViewById(R.id.textView1);
// Retrieve the buttons declared by the xml layout
mButton1 = (Button)findViewById(R.id.button1);
mButton2 = (Button)findViewById(R.id.button2);
mButton3 = (Button)findViewById(R.id.button3);
mButton4 = (Button)findViewById(R.id.button4);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
shuffle();
}
private void initQuizData() {
mQuestionsList.add("Who is the actual CEO at Apple?");
mQuestionsList.add("Who is the actual CEO at Microsoft?");
mQuestionsList.add("Android is made by:");
mAnswersList.add("Steve Jobs");
mAnswersList.add("Steven Sinofsky");
mAnswersList.add("Tim Cook");
mAnswersList.add("Steve Ballmer");
}
private void shuffle() {
mCurrentQuestion = mQuestionsList.get(mRandom.nextInt(mQuestionsList.size()));
mQuestionsList.remove(mCurrentQuestion);
mQuestionTv.setText(mCurrentQuestion);
mAnswersList.add("Steve Jobs");
mAnswersList.add("Steven Sinofsky");
mAnswersList.add("Tim Cook");
mAnswersList.add("Steve Ballmer");
mButton1.setText(mAnswersList.get(mRandom.nextInt(mAnswersList.size())));
mAnswersList.remove(mButton1.getText());
mButton2.setText(mAnswersList.get(mRandom.nextInt(mAnswersList.size())));
mAnswersList.remove(mButton2.getText());
mButton3.setText(mAnswersList.get(mRandom.nextInt(mAnswersList.size())));
mAnswersList.remove(mButton3.getText());
mButton4.setText(mAnswersList.get(mRandom.nextInt(mAnswersList.size())));
mAnswersList.remove(mButton4.getText());
}
private boolean validateAnswer(String question, String answer) {
if(question.equals("Who is the actual CEO at Apple?")) {
if(answer.equals("Tim Cook")) {
return true;
} else {
return false;
}
} else if (question.equals("Android is made by:")) {
return false;
} else if (question.equals("Who is the actual CEO at Microsoft?")) {
if(answer.equals("Steve Ballmer")) {
return true;
} else {
return false;
}
}
return false;
}
public void onClick(View v) {
Toast toast;
if(validateAnswer(mCurrentQuestion, ((Button)findViewById(v.getId())).getText().toString())) {
toast = Toast.makeText(this, "Good!", Toast.LENGTH_SHORT);
} else {
toast = Toast.makeText(this, "Too bad!", Toast.LENGTH_SHORT);
}
if(mQuestionsList.size()>0) {
toast.show();
shuffle();
} else {
toast.show();
}
}
}
The associated layout main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, SoRandomArray"
/>
<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=""
/>
<Button
android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=""
/>
<Button
android:id="@+id/button3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=""
/>
<Button
android:id="@+id/button4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="" />
</LinearLayout>
You'll have to correct some issue with the randomizing of the buttons, that's not state-of-the-art but that's the idea and it will give you a start…