user2711428
user2711428

Reputation: 1

Android Java Hyper Link for IF Else

I think this is the best place to post this. Could not find any good site to ask this. But here goes,

I am making a simple app, and I need the text to link to a webpage. The App randomly generates a set word each time the button is pressed. Here is the code.

This is a small amount of the code but it continues down for about 30 more if else.

public void onClick(View arg0) {
            // When the Button is Clicked
        String answer = "";

        //Random Answer Yes, No, or Maybe
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(30);

        if (randomNumber == 0) {
            answer = "TEXT HERE!";

        }
        else if (randomNumber == 1) {
            answer = "TEXT HERE!";

        }
        else if (randomNumber == 2) {
            answer = "TEXT HERE!";
        }
        else if (randomNumber == 3) {
            answer = "TEXT HERE!";
        }

how would I go about linking those words to a webpage? The links will be different though. So not all of the words will link to the same page. I have had a look around online but I don't think it would work great using else if statements

Upvotes: 0

Views: 132

Answers (1)

Bastian
Bastian

Reputation: 171

How about using an enumeration with all the information you want stored in it. Like so:

public enum Answer {
     ANSWER1("TEXT HERE", "http://link.to.some/page")
     ANSWER2("TEXT HERE!", "http://link.to.some/other/page")

     private String answer;
     private String link;

     private Answer (String text, String link) {
         this.answer = text;
         this.link = link;
     }
     // plus getter and setter if you like
}

Upvotes: 2

Related Questions