TextView 不改变它的文本

TextView doesn't change its text

您好,我的 TextView 有问题。这是我的代码

final TextView username = (TextView) _v.findViewById(R.id.username);
final LinearLayout linear7 = (LinearLayout) _v.findViewById(R.id.linear7);
final TextView date = (TextView) _v.findViewById(R.id.date);
final TextView msg = (TextView) _v.findViewById(R.id.msg);
final TextView username2 = (TextView) _v.findViewById(R.id.username);
final LinearLayout linear72 = (LinearLayout) _v.findViewById(R.id.linear7);
final TextView date2 = (TextView) _v.findViewById(R.id.date);
final TextView msg2 = (TextView) _v.findViewById(R.id.msg);
final LinearLayout linear999 = (LinearLayout) _v.findViewById(R.id.linear999);
final LinearLayout linear9992 = (LinearLayout) _v.findViewById(R.id.linear9992);

username.setText(map1.get((int)_position).get("name").toString());
msg.setText(map1.get((int)_position).get("msg").toString());
date.setText(map1.get((int)_position).get("date").toString());
username2.setText(username.getText().toString());
msg2.setText(msg.getText().toString());
date2.setText(date.getText().toString());

如您所见,username2 应与 username 具有相同的文本。但是,它只有我在设计器中为它提供的文本。请帮忙。

我认为你不能更改文本,因为你将它声明为最终变量。在什么情况下需要将此代码块声明为最终代码块。在没有我的第一个建议的情况下尝试一下。

final TextView username = (TextView) _v.findViewById(R.id.username);
final TextView username2 = (TextView) _v.findViewById(R.id.username);

它们具有相同的视图 ID。您必须更改 username2 视图 ID。

看看你的idTextView是一样的。改成2号连接的idTextView

 final TextView msg = (TextView) _v.findViewById(R.id.msg);

 final TextView msg2 = (TextView) _v.findViewById(R.id.msg); <--- change this ID

我只是将两个不同用户(Maxitors 和 Mike)提到的两个正确问题合并为一个答案:

两对的Textview是同一个元素

  • Username 和 Username2 有一个 id R.id.username
  • msg 和 msg2 有一个 id R.id.msg

这会导致它们在您对它们进行函数调用时引用相同的元素。例如,将 username2 的文本设置为 username 的文本不会产生明显的效果,因为您只是将元素 R.id.username 的文本设置为其自身。

要解决此问题,您需要将 Username2 和 msg2 分配给正确的 ID,无论它们是什么。 (看看你的命名方案,我怀疑它们是 R.id.username2 和 R.id.msg2 )