lundi 7 juin 2021

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.");


    }
}



Aucun commentaire:

Enregistrer un commentaire