4

I got a problem with reading items from string-array one by one. For example, i got string-array with 10 items:

<string-array name="arr">
    <item>First</item>
    <item>Second</item>
    <item>...</item>
    <item>Tenth</item>
</string-array>

So i know how to display items randomly, im using this code

Resources res = getResources();

myString = res.getStringArray(R.array.arr);

int length=myString.length;
int index=rgenerator.nextInt(length);
String q = myString[index];

tv = (TextView) findViewById(R.id.text);
tv.setText(q);

And in TextView on every button click it displays random item from array.

Problem is, how to make display item from string-array not randomly. Like, it starts from displaying First, then on click it displays Second, and so on untill end of array.
Please help!

4 Answers 4

4

You can't initialize your testArray field this way, because the application resources still aren't ready. Change the code to:

package com.xtensivearts.episode.seven;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class Episode7 extends ListActivity {
String[] mTestArray;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // Create an ArrayAdapter that will contain all list items
    ArrayAdapter<String> adapter;

    mTestArray =  = getResources().getStringArray(R.array.testArray);    

    /* Assign the name array to that adapter and 
    also choose a simple layout for the list items */ 
    adapter = new ArrayAdapter<String>(
    this,
    android.R.layout.simple_list_item_1,
    mTestArray);

    // Assign the adapter to this ListActivity
    setListAdapter(adapter);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Declare a variable

int currentIndex=0;

outside this onClick method.

In the

onClick(View v)
{
//Verify if only that btn is clicked
{
tv.setText(myString[(currentIndex++)%(myString.length)]);
}
}

Hope it works.

2 Comments

Thank you, man! it worked! My mistake was my counter was inside onClick method.. Thanks again!
It will crash after n clicks with IndexOutOfBoundsException (n = array.lenght). Change tv.setText(myString[currentIndex++]); to tv.setText(myString[currentIndex++ % myString.length]);
2

try

int i=0;
String q = myString[i];
i++;

Comments

1

strings.xml

<string-array name="month">
    <item>Jan</item>
    <item>Feb</item>
    <item>Mar</item>
    <item>Apr</item>
    <item>May</item>
    <item>Jun</item>
    <item>Jul</item>
    <item>Aug</item>
    <item>Sep</item>
    <item>Oct</item>
    <item>Nov</item>
    <item>Dec</item>
</string-array>

java

for (int i = 0; i < getResources().getStringArray(R.array.month).length; i++) {
    Log.e("Array",""+getResources().getStringArray(R.array.month)[i]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.