tags:

views:

39

answers:

2

Hi, what I am trying to do is to display an item on text on the screen and then following the user input chage that text to the next/previous item.

I have created a string arry with all the items in (xml) I have then called the array in the Java code and set up the OnclickListners. My problem is that I don't know what is write in the switch case(s) to make the screen display the next/last item in the list.

Any suggestions?

A: 

Hi,

Your question is not so clear, I suggest you show us your code (how you create the array, the TextView, the button), it will be easier to help you complete it.

What I would do to iterate a String array would be something like this:

String[] myArray={"s1","s2","s3"};
TextView myTextView; //suppose myTextView has already been created and added to your layout
int currentIndex=0;

private void onClick(View v) {
    if (currentIndex<myArray.length) {
        myTextView.setText(myArray[currentIndex]);
        currentIndex++;
    }
}
Sebastien
ROB, in order to help, I need to see how you create the array in the XML. It would also be useful to tell us what line causes the RunTime exception, and what is exactly this exception (use Eclipse debug mode for this).
Sebastien
A: 

Thanks Sebastien, I have used your code as follows, however I get a run time error and in closes, I'm sure it is something obvious, but i'm new to this and get see it, thanks for you help.

public class oneliners extends Activity implements OnClickListener { Resources res = getResources(); String[] arr= res.getStringArray(R.array.arr); int currentIndex=0; TextView tv = (TextView)findViewById(R.id.tv);

@Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    View next = findViewById(R.id.next);
    next.setOnClickListener(this);
    View last = findViewById(R.id.last);
    last.setOnClickListener(this);

    tv.setText(arr[0]);
     }




     @Override
     public void onClick(View next) {   
     if (currentIndex<arr.length) {
          tv.setText(arr[currentIndex]);
          currentIndex++;

 }
 }

}

Rob