如何使用循环将文本设置为android中的textViews

时间:2016-07-14 15:48:30

标签: java android

此处列表是一个数组列表。最新发生的是文本被覆盖并始终显示最后一个值。列表的大小是4。

for (int i = 0; i < list.size(); i ++){
  TextView name = (TextView) convertView.findViewById(R.id.user_names);
  name.setText(list.get(i).toString());
  TextView email = (TextView) convertView.findViewById(R.id.user_emails);
  email.setText(list.get(i).toString());
}

5 个答案:

答案 0 :(得分:2)

尝试使用此解决方案,例如:

private int[] food_list_dates = {
        R.id.date_1,
        R.id.date_2,
        R.id.date_3,
        R.id.date_4,
        R.id.date_5,
        R.id.date_6,
        R.id.date_7,
        R.id.date_8,
        R.id.date_9,
        R.id.date_10,
        R.id.date_11,
        R.id.date_12,
        R.id.date_13,
        R.id.date_14,
};

...

for (int i = 0; i > foodList.size(); i++) {
    ((TextView) findViewById(food_list_dates[i])).setText( i + "" );
}

答案 1 :(得分:0)

只需追加所有内容。并且提到的@siddhesh dighe不要在循环中使用TextView初始化。

TextView name = (TextView) convertView.findViewById(R.id.user_names);
TextView email = (TextView) convertView.findViewById(R.id.user_emails);

for (int i = 0; i < list.size(); i ++){
  name.append(list.get(i).toString());     
  email.append(list.get(i).toString());
}

答案 2 :(得分:0)

  TextView name = (TextView) convertView.findViewById(R.id.user_names);
  TextView email = (TextView) convertView.findViewById(R.id.user_emails);

  for (int i = 0; i < list.size(); i ++){
   name.append(list.get(i).toString());
   email.append(list.get(i).toString());
  }

答案 3 :(得分:0)

尝试使用append()代替:

TextView name = (TextView) convertView.findViewById(R.id.user_names);
TextView email = (TextView) convertView.findViewById(R.id.user_emails);
for (int i = 0; i < list.size(); i ++){
  if(i==0)
  {
    name.append(list.get(i).toString());
    email.append(list.get(i).toString());
  }else {
    name.append("," + list.get(i).toString());
    email.append("," + list.get(i).toString());
  }
}

答案 4 :(得分:0)

您的数据有touch -r oldfile newfile而且您有ArrayList,因此您可以ListView使用ArrayAdapter并将Arraylist传递给它。

listView

在此处查看完整的tutorial