Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p><strong>Update:</strong></p> <p>A quick-and-dirty implementation (without adapter nor ViewHolder, <em>etc.</em>):</p> <pre><code>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&lt;String&gt; mQuestionsList; private String mCurrentQuestion = null; private List&lt;String&gt; 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&lt;String&gt;(); mAnswersList = new ArrayList&lt;String&gt;(); 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()&gt;0) { toast.show(); shuffle(); } else { toast.show(); } } } </code></pre> <p>The associated layout main.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, SoRandomArray" /&gt; &lt;Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="" /&gt; &lt;Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="" /&gt; &lt;Button android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="" /&gt; &lt;Button android:id="@+id/button4" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>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…</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload