gpt4 book ai didi

java - 如何在 TextView 中动态显示字符串列表中的项目

转载 作者:行者123 更新时间:2023-11-30 02:23:07 25 4
gpt4 key购买 nike

我想将列表中的项目显示给 TextView 。

wordlist.java 是

public class WordsList {

List<String> set1 = new ArrayList<>(Arrays.asList("sane", "said",
"dean", "ideas", "deans", "anise", "naiades", "sand", "aide",
"dais", "saned", "aside", "sedan", "idea", "aids", "ands",
"naiad", "aides", "naiads"));
// 19 words

List<String> set2 = new ArrayList<>(Arrays.asList("doer", "lord",
"rode", "role", "drool", "older", "flooder", "odor", "lore",
"rood", "fore", "rodeo", "folder", "floored", "door", "roof",
"redo", "ford", "floor", "roofed"));
// 20 words
List<String> set3 = new ArrayList<>(Arrays.asList("mead", "dale",
"lead", "dual", "lamed", "mauled", "medulla", "lade", "male",
"alum", "maul", "mall", "ladle", "malled", "dame", "made", "lame",
"laud", "meal", "medal", "allude"));
// 21 words

其实这些就是拼图的答案,大概有25个拼图(从set1到set 25)

当用户点击放弃时。它进入该 Activity 中的游戏结束 Activity ,我有按钮“显示遗漏的单词”,现在按下此按钮时,我想显示相应列表中的项目。

为了显示文字,我创建了一个带有一些 TextView 的 xml 布局。

LinearLayout myLayout;    myLayout = (LinearLayout) findViewById(R.id.tvLayout);
myTextViewList = new ArrayList<>();

for (int i = 0; i < myLayout.getChildCount(); i++)
if (myLayout.getChildAt(i) instanceof TextView)
myTextViewList.add((TextView) myLayout.getChildAt(i));
and when to set text
WordsList w = new WordsList();
TextView tv = myTextViewList.get(counter);
tv.setText(w.set1);

Here is my logcat

 01-28 20:29:42.105  11122-11122/rpa.screening.spellathon E/AndroidRuntime﹕ FATAL EXCEPTION: main    Process: rpa.screening.spellathon, PID: 11122    java.lang.NullPointerException            at rpa.screening.spellathon.GameOver_Screen$2.onClick(GameOver_Screen.java:44)            at android.view.View.performClick(View.java:4487)            at android.view.View$PerformClick.run(View.java:18746)            at android.os.Handler.handleCallback(Handler.java:733)            at android.os.Handler.dispatchMessage(Handler.java:95)            at android.os.Looper.loop(Looper.java:149)            at android.app.ActivityThread.main(ActivityThread.java:5257)            at java.lang.reflect.Method.invokeNative(Native Method)            at java.lang.reflect.Method.invoke(Method.java:515)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)            at dalvik.system.NativeStart.main(Native Method)

EDIT 2 :Gameover_screen

missedWords.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent iin = getIntent();
Bundle b = iin.getExtras();


if (b != null) {

String passed_score = (String) b.get("score");
displayScore.setText(passed_score);
int passed_set = (int) b.get("set"); // line 44
String s = Integer.valueOf(passed_set).toString();


Intent ii = new Intent(GameOver_Screen.this, MissedWords.class);
ii.putExtra("sEt", s);
startActivity(ii);
}
}
});

最佳答案

解决了我的问题。

真正的问题在这里,

tv.setText(w.set1);

但是当我把它改成,

String set1 = w.set1.get(i);
tv.setText(set1);

问题解决了。

关于java - 如何在 TextView 中动态显示字符串列表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28195086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com