Sophia Woon
Sophia Woon

Reputation: 9

Why random words from array doesn't show when I click the button?

I wanted to create a simple app that shows words/sentences randomly from a list that I've created every time the user click on the button.

Android Studio doesn't show any error but nothing happens when I click on the button.

Appreciate your help. Thank you

Here's my code. MainActivity.java


package com.example.arraytesting4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.lang.String;
import java.lang.StringBuilder;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<String> SList;
    TextView Sentence;

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

        Sentence = (TextView) findViewById(R.id.Sentence);

    }


public void buttonClicked(View v){
    printJoke();
}

public void printJoke() {
    StringBuilder SentenceStringBuilder = new StringBuilder();
    for (String s : SList) {
        SentenceStringBuilder.append(s + "\n");
    }
    Sentence.setText(SentenceStringBuilder.toString());
}
}

In SList.java


package com.example.arraytesting4;
import java.util.*;

import java.util.ArrayList;

public class SList extends MainActivity {
    public void Sentence(){

        ArrayList<String> SList = new ArrayList<String>();

        SList.add("Apple");
        SList.add("Banana");
        SList.add("Ciku");
        SList.add("Danny ate the others.");


    }
}

Upvotes: 1

Views: 59

Answers (4)

Sanket Shah
Sanket Shah

Reputation: 36

you just need to write following code in your onCreate after importing button,

clickMe.onClickListener((View v) -> {
    Sentence.setText(getRandomFromList(getSampleList())); 
});

Now add below two methods to create List & get a random from List,

private ArrayList<String> getSampleList(){
     ArrayList<String> list = new ArrayList();
     list.add("Apple");
     list.add("Banana");
     list.add("Water Melon");
     list.add("Fruit X");
     list.add("Fruit Y");
     return list;
 }
 
 private String getRandomFromList(ArrayList<String> list){
     Random rand = new Random();
     return list.get(rand.nextInt(list.size()));
 }

Upvotes: 0

muratcanyeldan
muratcanyeldan

Reputation: 109

You have to add listener to your button. So your buttons can know what they will do when someone click on them.

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

    Sentence = (TextView) findViewById(R.id.Sentence);
    Button myButton = (Button) findViewById(r.id.myButtonId);

    myButton.setOnClickListener((View v) -> {
       buttonClicked(); //or you can delete buttonClicked Method and call printJoke Method here
    });

}

Upvotes: 0

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20646

Your hierarchy here doesn't have any sense. I don't know what you tried or thought but this is incorrect.

If you extends your MainActivity then your Slist is also an Activity, is your project even running?

There are different problems here :

  1. You don't use Sentence() method anywhere
  2. You are extending MainActivity to create a method of an ArrayList

An easy way should be create a method to initialise that ArrayList inside your MainActivity and then it should work.

Create a method to initialise the List and call this method inside onCreate()

private void initialiseArray(){
        SList = new ArrayList<>();
        SList.add("Apple");
        SList.add("Banana");
        SList.add("Ciku");
        SList.add("Danny ate the others.");
    }

Also remember to remove the Slist class.

Note: I don't know if you just want to show one sentence on the TextView or all the items in the ArrayList, in this case how you did it would show all the items.

Upvotes: 1

frendy wijaya
frendy wijaya

Reputation: 77

You haven't declare your button yet.

do something like this

Button *your button* = (Button) findViewById(R.id.*your button id*);

then, instead of creating buttonClicked function, create button on click listener in your onCreate function. something like this

*your button*.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 printJoke()
            }
        });

Upvotes: 0

Related Questions