4

Im trying to make a simple quote app, but every time I hit my back button after my next button it will go to the next array and not backwards. Im using an int num to keep track of with quote the user is on. here is my back button

package me.me.quote;


public class Startme extends Activity {
 Random gen= new Random();
int num;

 String[] quotes = new String[15];





public void next(View N){
         quotes[0]= "Love is timeless";
            quotes[1]= "I hate you";
            quotes[2]= "arggg!";
            quotes[3]= "The end is near";
            quotes[4]= "Leave be";

            if(num == 5){
                num =0;}



           TextView text = (TextView) findViewById(R.id.Quote);
           text.setText(quotes[num++]);



           TextView number = (TextView) findViewById(R.id.Qn);
            number.setText(""+num);

        }
public void back(View B){

 quotes[0]= "Love is timeless";
   quotes[1]= "I hate you";
   quotes[2]= "arggg!";
   quotes[3]= "The end is near";
   quotes[4]= "Leave be";

 TextView text = (TextView) findViewById(R.id.Quote);
 text.setText(quotes[num--]);

 TextView number = (TextView) findViewById(R.id.Qn);
    number.setText(""+num);}




  public void rand(View G)  {

 num = gen.nextInt(5);

  quotes[0]= "Love is timeless";
  quotes[1]= "I hate you";
  quotes[2]= "arggg!";
  quotes[3]= "The end is near";
  quotes[4]= "Leave be";
  TextView text = (TextView) findViewById(R.id.Quote);
  text.setText(quotes[num]);

TextView number = (TextView) findViewById(R.id.Qn);
number.setText(""+num);         
}


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

    TextView Dark=(TextView)findViewById(R.id.Quote); 
    Typeface face=Typeface.createFromAsset(getAssets(), "fonts/font1.ttf"); 
    Dark.setTypeface(face); 


}


}
4
  • Can you post the whole class? Commented Jul 16, 2012 at 22:02
  • 3
    Why you are having two arrays ? Cant you simply mention single Global Array Commented Jul 16, 2012 at 22:07
  • 1
    there's the entire class. I am very new to java and all help is thanked very much =] Commented Jul 16, 2012 at 22:18
  • 1
    I have an small application for you and provided the code which will help you. If it helps and solved your problem please accept my answer and up vote me . Thanks Commented Jul 16, 2012 at 22:36

1 Answer 1

4

//LAYOUT FILE text.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:text="VISIBLE EVERY TIME"
            android:visibility="visible" />

        <Button
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:text="NEXT" >
        </Button>

        <Button
            android:id="@+id/next"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:text="BACK" >
        </Button>

    </RelativeLayout>

///ACTIVITY CLASS

 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 Test extends Activity {

        Button next;
        Button back ;
        TextView quoteText;
        int currentSelectedQuote=0;
        String[] quote={"QUOTE1","QUOTE2","QUOTE3","QUOTE4","QUOTE5",};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            setContentView(R.layout.text);

            quoteText=(TextView)findViewById(R.id.textView);


            quoteText.setText(quote[currentSelectedQuote]);

             next=(Button)findViewById(R.id.next);

             next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    currentSelectedQuote++;


                    if(currentSelectedQuote == 5){
                        currentSelectedQuote =0;}

                    quoteText.setText(quote[currentSelectedQuote]);

                }
            });
             back=(Button)findViewById(R.id.back); 

             back.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if(currentSelectedQuote==0)
                    {
                        currentSelectedQuote=4;
                    }
                    else
                    {
                    currentSelectedQuote--;
                    }

                    quoteText.setText(quote[currentSelectedQuote]);

                    // TODO Auto-generated method stub

                }
            });





        }

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

2 Comments

Thank you so much Salman. such a simple answer but yet it makes me so happy. I will vote up as soon as my rep hits 15. Thanks again
I have helped you to move to 11 from 6 :) awaiting for your 15 rep. so that you can accept my answer as well as up vote me . Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.