mercredi 11 mai 2022

Display A Random Quote and Author from Array List in a New activity On Button Click

I'm trying to create a mood checker application. Where when the user clicks on a mood, it would display a quote in a new activity. My application runs but the quote is not displayed. I think there might be a problem with my try catch method, but I don't really know how to resolve this.

public class MainActivity extends AppCompatActivity {
public ImageButton awesome, good, neutral,sad, awful;
public int index;
public ArrayList<Quote> awesome_List;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // DECLARE MOOD BUTTONS//
    awesome=(ImageButton) findViewById(R.id.btn_awesome);
    good=(ImageButton) findViewById(R.id.btn_good);
    neutral=(ImageButton) findViewById(R.id.btn_neutral);
    sad=(ImageButton)findViewById(R.id.btn_sad);
    awful=(ImageButton)findViewById(R.id.btn_awful);

    //Create Array List for each mood quote and author


    //AWESOME MOOD//
    String[] awesomeQuoteArray = {
            "“Happy people don\\'t have the best of everything, they make the best of everything",
            "“Most people are about as happy as they make up their minds to be”.",
            "“A happy person is happy, not because everything is right in their life but because their attitude towards everything in life is right”.",
            "“Make someone happy. Make just one someone happy and you will be happy too”",
            "“The beauty of life does not depend on how happy you are, but how happy others are because of you”."
    };

    String[] awesomeAuthorArray={
            "Marcel Proust",
            "Eckhart Tolle",
            "Adi Shankara",
            "Marcus Tullius Cicero",
            "Marcus Aurellius"
    };
    awesome_List=new ArrayList<>();
    addAwesomeToQuoteList(awesomeQuoteArray,awesomeAuthorArray);

    // generate awesome random quotes
    final int awesomeQuoteLength = awesome_List.size();
    index=getRandomQuote(awesomeQuoteLength-1);
    addAwesomeToQuoteList(awesomeQuoteArray,awesomeAuthorArray);

    awesome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            sendAwesomeData(); 

    }
    });
   }
   
public void sendAwesomeData(){
    String AwesomeText="";
    try {
        System.out.println(AwesomeText.charAt(awesome_List.size()));

        String hello ="hello";

        AwesomeText= awesome_List.get(index).toString();


    }catch (StringIndexOutOfBoundsException e){
        System.out.println("String index out of bounds. String length:" + AwesomeText.length());
    }
    String AwesomeOutputText = AwesomeText;
    Intent awesomeIntent= new Intent(MainActivity.this, awesome_mood_quotes.class);

    awesomeIntent.putExtra(awesome_mood_quotes.QUOTE,AwesomeOutputText );
    startActivity(awesomeIntent);
}


public int getRandomQuote(int length){
    return(int) (Math.random()*length)+1;
}

}


Second Activity Code

public class awesome_mood_quotes extends AppCompatActivity {
public static final String QUOTE= "QUOTE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_awesome_mood_quotes);

    TextView awesomeQuote=(TextView) findViewById(R.id.awesome_quote_text);

    Intent intent= getIntent();
    String AwesomeText=intent.getStringExtra(QUOTE);
    awesomeQuote.setText(AwesomeText);



}

}




Aucun commentaire:

Enregistrer un commentaire