java - 将 arraylist 中的每个值显示到每个 textView Android

标签 java android arrays arraylist textview

我试图将数组列表中的每个值显示给我手动创建或固定的textView的每个textView

    totalCardPerPage = 30;
    cardNumber = 1;
    //looping to determine textView id in my xml
    for(int x=1;x<=totalCardPerPage;x++){
        //textView id
        textId = "text"+cardNumber;
        int id = getResources().getIdentifier(textId, "id", "com.example.radioexample");
        TextView currcell = (TextView) findViewById(id);
        //get array size
        int arrayListSize = arrayListPage1.size();

        //looping to show each value in textView
        for(int y=0; y<=arrayListSize; y++){
            currcell.setText(arrayListPage1.get(y).toString);
            currcell.setBackgroundColor(Color.RED);
        }
        cardNumber++;
    }

问题是循环显示 textView 中的每个值。

运行此代码时出现错误

我通过 arraylist 中的循环实现了这一点,数组中的所有值仅聚集在一个 textView 中

如果我的数组中有5个值,每个值都会显示在每个textView中。

例如,

 arraylist {a,b,c,d,e};
 text1.setText(a);
 text2.setText(b);
 text3.setText(c);
 text4.setText(d);
 text5.setText(e);

有人知道怎么做吗?

谢谢

最佳答案

这个问题很旧但是是 Google 搜索中的第一个结果。

要完成这项工作,您必须在循环中调用 arraylist 的所有值,并在特殊的 Textview 上设置,如下代码:

如果你有这个数组列表和 TextView

arraylist {a,b,c,d,e};

 TextView []tv=new TextView[5];
tv[0]=(TextView)findViewById(R.id.____);
tv[1]=(TextView)findViewById(R.id.____);
tv[2]=(TextView)findViewById(R.id.____);
tv[3]=(TextView)findViewById(R.id.____);
tv[4]=(TextView)findViewById(R.id.____);

//循环将数组列表加载到特殊的 Textview 中

 for(int i=0;i<arraylist.size();i++)
                {
                   tv[i].setText(arraylist.get(i).toString());
                }

关于java - 将 arraylist 中的每个值显示到每个 textView Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23222654/

相关文章:

java - 无法建立双向 SSL 连接(找不到别名的公钥)

java - 如何限制 HtmlUnit 的历史大小?

android - 如果已连接,SignalR Android Hub连接无法启动

javascript - 如果 else 没有按预期工作,我的逻辑有问题吗?

javascript - jQuery解析json并获取元素,得到未定义

java - 如何检查可执行 JAR 是否已在另一个 JVM 中完成

java - WakeLock 会导致手机重启吗?

android - Firebase 数据库 IllegalStateException : Fragment not attached to Activity

android - Compose with Hilt last version (2.35) 问题: Unable to find method

arrays - SGE 数组作业和 R