0

I am creating this activity where using import java.util.Random;, it fetches a question from the string-array inside the arrays.xml. The item fetches from the array is displayed in a textview at the top of my layout. What I want to achieve here is having a matching background from every item fetches from the string-array. My approach to this was to match the generated index fetched from the questions array and create an if statement.If item=0, then this background shown in * in the code. This is what I implemented so far, open to any suggestions or improvements to my code:

Activity

questions = getResources().getStringArray(R.array.questions);

@SuppressLint("NewApi") private void getQuestion(){
    TextView questionText = (TextView) findViewById(R.id.question);
    Random rand = new Random();

    int maxIndex = questions.length;
    int generatedIndex = rand.nextInt(maxIndex);        
    if(generatedIndex == 1){
        **screenGame.setBackground(getResources().getDrawable(R.drawable.footballfindtheposition));
    }
    else
        screenGame.setBackground(getResources().getDrawable(R.drawable.mainbg));
    questionText.setText(questions[generatedIndex]);
}**

arrays.xml

<string-array name="questions">
    <item>Click on the player with the possition of a Center Back</item>
    <item>Click on the Player with the possition of Left Center Midfield</item>
    <item>Click on the blabla</item>    
</string-array>

I get a NullPointerException at the end of my code in LogCat

cmario
  • 605
  • 1
  • 7
  • 22

1 Answers1

1

You can define another array in arrays.xml containing the drawables. Make sure it is the same size as your questions array:

<string-array name="backgrounds">
    <item>@drawable/footballfindtheposition</item>
    <item>@drawable/midfieldfindtheposition</item>
    <item>@drawable/blabla...</item>    
</string-array>

Then in your code, load it as a TypedArray:

TypedArray backgrounds = getResources().obtainTypedArray(R.array.backgrounds);

There are different functions in TypedArray that allow you to interpret the contents as different things (e.g. resource ids, dimensions, text, integer numbers etc).

You can look up the value as a resource id using getResourceId(), which returns an int:

TypedArray backgrounds = getResources().obtainTypedArray(R.array.backgrounds);
int maxIndex = questions.length;
int generatedIndex = rand.nextInt(maxIndex);
screenGame.setBackground(getResources().getDrawable(backgrounds.getResourceId(generatedIndex,-1));
questionText.setText(questions[generatedIndex]);
backgrounds.recycle();

Doing it this way means you don't have to have a long if statement, and you can manage your background drawables and question in on place (arrays.xml)

samgak
  • 23,944
  • 4
  • 60
  • 82
  • is there a way to match an objectIndex in an arrayList with another objectIndex either on the same or another arrayList? – cmario Jul 13 '15 at 07:50
  • Iterate over the ArrayList using a for-each construct: http://stackoverflow.com/a/6700737/696391 – samgak Jul 13 '15 at 08:00