Ali Ahmed
Ali Ahmed

Reputation: 183

How to get array items one by one on Button click?

I am learning android studio, and I am a complete beginner. I have a textview and button on my activity, and i want that the textview takes strings from array in strings.xml and changes when i click the button.

I tried a for loop but it just gives one value and stops. This is the strings.xml file.

<resources>
<string name="app_name">myapp</string>

<string-array name="myarray">
    <item>Cow</item>
    <item>Pig</item>
    <item>Bird</item>
    <item>Sheep</item>
</string-array>
</resources>

This is the mainActivityfile

package com.mlx.myapp;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    Context context = this;
    String randomstr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] arrays = context.getResources().getStringArray(R.array.myarray);


        final TextView textview = (TextView) findViewById(R.id.textview);
        for (int i = 0; i < arrays.length; i ++ ) {
            randomstr = arrays[i];
        }

        final Button getrandom = (Button) findViewById(R.id.getrandom);
        getrandom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textview.setText(randomstr);
            }
        });
    }
}

Now when I run it, it just displays the item "sheep" from on first button click and then it doesn't change, but i want it to change on every press.

Upvotes: 0

Views: 1058

Answers (1)

Hichem BOUSSETTA
Hichem BOUSSETTA

Reputation: 1857

  • You must remove the loop because it just assigns the last value of the array in randomstr.
  • you have afterwards to define an iterator inside the click method, and update this iterator at each click so you can display the next value

    public class MainActivity extends AppCompatActivity { Context context = this; String randomstr; int iterator = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        String[] arrays = context.getResources().getStringArray(R.array.myarray);
    
    
        final TextView textview = (TextView) findViewById(R.id.textview);
        for (int i = 0; i < arrays.length; i ++ ) {
            = arrays[i];
        }
    
        final Button getrandom = (Button) findViewById(R.id.getrandom);
        getrandom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                //Read the current value
                randomstr = arrays[iterator];
    
                //Display the current value
                textview.setText(randomstr);
    
                //Increment the iterator to read the next value
                iterator = iterator+1;
    
                //If we reached the length of the array, we reset the iterator
                if(iterator >= arrays.length) {
                   iterator = 0;
              }
            }
        });
      }
    }
    

Upvotes: 3

Related Questions