如何动态显示textviews中字符串列表中的项目

时间:2015-01-28 14:45:00

标签: java android arraylist

我想将列表中的项目显示为textviews。

wordslist.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个谜题(从第1组到第25组)

当用户点击放弃时。它进入游戏活动的那个活动我有按钮&#39;显示错过的单词&#39; ,现在按下此按钮时,我想显示相应列表中的项目。

用于显示我创建带有一些textview的xml布局的单词。     

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);
            }
        }
    });
以及何时设置文本     
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));

这是我的logcat          

WordsList w = new WordsList();
TextView tv = myTextViewList.get(counter);
tv.setText(w.set1);

编辑2: Gameover_screen

 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)

1 个答案:

答案 0 :(得分:1)

解决了我的问题。

实际问题在这里,

tv.setText(w.set1);

但是当我把它改成时,

String set1 = w.set1.get(i);
tv.setText(set1);
问题解决了。