我是Android编程的新手,过去3个月一直在观看youtube视频,试图通过反复试验来学习。我被卡住了。
我试图做的就是显示我的" JokeList.java"单击屏幕上的按钮后,在TextView(robotJoke)中。有人可以帮助我吗?
这是我的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>
答案 0 :(得分:1)
您应循环遍历列表并逐个附加列表元素。
public void printJoke(){
StringBuilder jokeStringBuilder = new StringBuilder();
for (String s : jokeList) {
jokeStringBuilder.append(s + "\n");
}
robotJoke.setText(jokeStringBuilder.toString());
}
另一个错误是您需要将buttonClicked()
函数中的视图传递给buttonClicked(View v)
答案 1 :(得分:0)
首先定义textview,而不是从ArrayList中获取所有数据并设置textview,就像这个例子一样
ready
答案 2 :(得分:0)
您的JokeList不需要扩展MainActivity。 添加构造函数来填充数组。
public class JokeList{
private ArrayList<String> jokeList;
public JokeList(){
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.");
}
public ArrayList<String> jokes(){
return jokeList;
}
}
您的活动: 创建一个JokeList对象并向他询问ArrayList。
public class MainActivity extends Activity{
ArrayList<String> jokeList;
JokeList myJokes;
TextView robotJoke;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
robotJoke = (TextView) findViewById(R.id.robotJoke);
myJokes = new JokeList();
}
public void buttonClicked(){
printJoke();
}
public void printJoke(){
jokeList = myJokes.jokes();
String jokeString = jokeList.toString();
robotJoke.setText(jokeString);
}
}
答案 3 :(得分:0)
好的,我们有点不同......
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);
jokeList = jokes();
}
public void buttonClicked(){
printJoke();
}
public void printJoke(){
Random jokeNumber = new Random();
String jokeString = jokeList.get(jokeNumber.nextInt() % jokeList.size()).toString();
robotJoke.setText(jokeString);
}
public ArrayList<String> 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.");
return jokeList;
}
}
答案 4 :(得分:0)
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(View v){ // Missing View paramter
printJoke();
}
public void printJoke(){
//from @Ahmed Hegazy
StringBuilder jokeStringBuilder = new StringBuilder();
for (String s : jokeList) {
jokeStringBuilder.append(s + "\n");
}
robotJoke.setText(jokeStringBuilder.toString());
}
}
答案 5 :(得分:-1)
试用此代码:
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(View view){
jokes();
printJoke();
}
public void printJoke(){
String jokeString = jokeList.toString();
robotJoke.setText(jokeString);
}
public void jokes(){
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.");
String jokeString = jokeList.toString();
Log.d("JOKES",jokeString);
}