Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do i use one string array entry based upon its position within the array (Android)?
    primarykey
    data
    text
    <p>I'm trying to produce a simple oracle app where when you ask the app a question it spits out an answer based on a random number.</p> <p>I've done some research and it seems to me the best practice way of doing things is to create a resource for my answers containing a string array, i can then type my various answers in to the array.</p> <p>In my Java code in my main activity i can then generate a random number upon the click of a button. This random number can then correspond to the number of the index.</p> <p>My problem comes when i try to piece the random number and the facility to access the string array together in code.</p> <p>Here is a copy of my answerStrings.xml file</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;string-array name="answers"&gt; &lt;item&gt;Yes&lt;/item&gt; &lt;item&gt;No&lt;/item&gt; &lt;item&gt;Maybe&lt;/item&gt; &lt;item&gt;Quite possibly&lt;/item&gt; &lt;/string-array&gt; &lt;/resources&gt; </code></pre> <p>Here is my java code:</p> <pre><code>import java.util.Random; import android.app.Activity; import android.content.res.Resources; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Vibrator; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { TextView outputText; MediaPlayer mpGobble; Vibrator vibr; String[] answers; private Random myRandom = new Random(); int randomNumber; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //find the output text view for use within the activity outputText = (TextView)findViewById(R.id.textView01); outputText.setOnClickListener(this); //Declare an array of outputs Resources res = getResources(); String[] answers = res.getStringArray(R.array.answers); //--------^ Problem outputText.setText("Ask your question then click me"); } @Override public void onClick(View src) { switch(src.getId()){ case R.id.textView01: //creates a random number int randomNumber = myRandom.nextInt(3); String answer = answers[randomNumber]; outputText.setText(answer); break; } } } </code></pre> <p>I'm programming in Java with the eclipse IDE and as you have probably guessed i'm new to the game!</p> <p>All help is much appreciated!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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