gpt4 book ai didi

java - 单击按钮时如何在 TextView 中显示 ArrayList?

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:48 25 4
gpt4 key购买 nike

我是 Android 编程新手,过去 3 个月一直在观看 YouTube 视频,试图通过反复试验来学习。我被困住了。

我想做的就是在单击屏幕上的按钮后在 TextView (robotJoke) 中显示我的“JokeList.java”。有人可以帮我吗?

这是我的 MainActivity.java 文件

package com.example.wdoty.comedianrobot;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.lang.String;
import java.util.ArrayList;

public class MainActivity extends Activity{

ArrayList<String> jokeList;
TextView robotJoke;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
robotJoke = (TextView) findViewById(R.id.robotJoke);

}

public void buttonClicked(){
printJoke();
}

public void printJoke(){
String jokeString = jokeList.toString();
robotJoke.setText(jokeString);
}


}

这是我的 ArrayList.java (JokeList.java)

package com.example.wdoty.comedianrobot;
import java.util.*;



public class JokeList extends MainActivity{

public void jokes(){

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

jokeList.add("Why couldn't the bicycle stand up?"+
"Because it was two tired");
jokeList.add("What do you call a cheese that isn't yours?"+
"Nacho Cheese!");
jokeList.add("Before I criticize a man, I like to walk a mile in his shoes."+
"That way, when I do criticize him, I'm a mile away and I have his shoes.");
jokeList.add("What do you call a fish with no eye?"+
"Fssshh");
jokeList.add("Have you heard the one about the Corduroy pillow?"+
"It's making HEADLINES!");
jokeList.add("What's red and bad for your teeth?"+
"A brick.");
jokeList.add("Why didn't the melons get married?"+
"Because they cantaloupe!");
jokeList.add("What did the cobbler say when a cat wandered into his shop?"+
"Shoe!");
jokeList.add("What did the Buddhist say to the hot dog vendor?"+
"Make me one with everything!");
jokeList.add("The face of a child can say it all"+
"especially the mouth part of the face.");
jokeList.add("Why did the cookie go to the hospital?"+
"Because he felt crummy.");
jokeList.add("I intend to live forever..."+
"So far, so good.");

}

}

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:id="@+id/imageView"
android:src="@drawable/android"
android:layout_centerHorizontal="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/robotText"
android:text="Hello. My name is Andy the Android I like telling jokes. Click the button below if you would like for me to tell you 1."
android:layout_below="@+id/imageView"
android:paddingTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_below="@id/robotText"
android:paddingTop="15dp"
android:text="Button"
android:layout_centerHorizontal="true"
android:onClick="buttonClicked" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/robotJoke"
android:layout_below="@id/button"
android:paddingTop="30dp"/>

</RelativeLayout>

最佳答案

您应该循环遍历列表并将列表中的元素一一追加。

public void printJoke(){
StringBuilder jokeStringBuilder = new StringBuilder();
for (String s : jokeList) {
jokeStringBuilder.append(s + "\n");
}
robotJoke.setText(jokeStringBuilder.toString());
}

另一个错误是您需要将 buttonClicked() 函数中的 View 传递为 buttonClicked(View v)

关于java - 单击按钮时如何在 TextView 中显示 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35751444/

25 4 0