Crazycriss
Crazycriss

Reputation: 315

Android: Random Word Generator

I have this so far but i cant fine whats wrong, sorry for the dumb question, im new to all this. I also posted the error it gives me for further reference

package com.example.randomword;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends Activity {

String[] quotes = new String[] {"q1", "q2", "q3"};
String randomQuote = quotes[(int) (Math.random() * quotes.length)];

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Make a new text view passing Activity object
TextView quote = new TextView(R.id.quote);
//Set a text into view

quote.setText(quotes);             <--- Error here "The method setText(CharSequence) in the type TextView is not applicable for the arguments (String[])"
//set the view into activity view container
setContentView(quote);

}
}

Upvotes: 1

Views: 3453

Answers (3)

Vineet Kosaraju
Vineet Kosaraju

Reputation: 5840

Quotes is an array of strings, not just one string. You want to use

quote.setText(randomQuote);

If you do want to set the value to the string array, for example spread by new lines you could do:

String quotesString = "";
for(String s : quotes) 
    quoteString += s + "\n";
quote.setText(quotesString);

Upvotes: 1

Infinite Recursion
Infinite Recursion

Reputation: 6557

You are passing a array of Strings to setText method of TextView. That is the problem.
quotes is declared as :

String[] quotes = new String[] {"q1", "q2", "q3"};

Then you wrote:

quote.setText(quotes);

You need to pass only one String to setText
either:

quote.setText(randomQuote);

or

quote.setText(quotes[i]); // where i is an integer denoting index of the element in quotes

Upvotes: 0

GrIsHu
GrIsHu

Reputation: 23638

Do not set the Array of Strings in the TextView besides set the String only as below You can not set the String array in TextView:

quote.setText(randomQuote);  

Upvotes: 3

Related Questions