0

simple question what do i need to add to this code so that when the button is pressed it replaces the text view with one of the strings from the array list?

    package com.

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;


    public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button b = (Button) findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               TextView tx = (TextView) findViewById(R.id.textView2);
               tx.setText("Hello");

           }
    });
    }
    }

    -----------------------------------------------------


    <?xml version="1.0" encoding="utf-8"?>
    <resources>

    <string name="hello">Hello World, main!</string>
    <string name="app_name">Ihavenever</string>

    <string-array
    name="list">
    <item>@string/girl</item>
    <item>@string/boy</item>
    <item>@string/man</item>
    <item>@string/women</item>
    <item>@string/dog</item>
    <item>@string/cat</item>
    </string-array>

    </resources>

so in a nutshell the app launches there is a button at the bottom once pushed it puts one of the items in the string array in the textview in the middle of screen and when you push it again it gives you another one and another one till there is none left and then restarts from the first one ?

1
  • I would suggest you to set an variable int i=0; . Then on click-listener, you put: b.setText(list[i++]); Commented Jan 6, 2013 at 20:49

1 Answer 1

1

Make

int counter = 0; 

global (outside onCreate()).

Then in onCreate() after declaring your button:

Button b = (Button) findViewById(R.id.button1);

Resources res = getResources();
final String[] list = res.getStringArray(R.array.list); //get the array
((TextView) findViewById(R.id.textView2)).setText (list [counter]); //set the initial message.

b.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               TextView tx = (TextView) findViewById(R.id.textView2);
               counter++;
               if (counter >= list.length)
                  counter = 0;

               tx.setText(list [counter]); //set the new message.

           }
    });
Sign up to request clarification or add additional context in comments.

5 Comments

@M0n5terBunny Based on what I can find, that does work, what is the issue?
i have "array could not be resolved or is not a field" error and where do i put my string array
You can put it in /res/values/strings.xml Make sure to clean your project too if R does not contain an array field.
right got it working cheers but when it gets to the end of the list it causes the app to crash is there anything to put on the end to make it restart from the beginning of the array
@M0n5terBunny What I had before should have restarted based on a specific length. I updated my answer so it uses the array's length instead.

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.