我想从一个对象中获取一个字符串值,该对象由列表中的一个 ImageView
和两个 TextView
组成,方法是单击该特定对象。但问题是,我无法获得我想要的字符串。
toast 消息应该显示第二个 TextView
的值,但它会显示任何它喜欢的内容。
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:orientation="vertical"
android:onClick="chitChat" >
<ImageView
android:id="@+id/photoImageView"
android:layout_width="300dp"
android:layout_height="200dp"
android:adjustViewBounds="true" />
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:text="Name" />
<TextView
android:id="@+id/uidTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
如果您有任何其他建议可以帮助我从单击的对象中获取任何对象特定值,我很想听听。
非常感谢您。
public void chitChat(View view) {
TextView otherUid = findViewById(R.id.uidTextView);
String othersUid = otherUid.getText().toString();
Toast.makeText(PartyActivity.this, othersUid, Toast.LENGTH_LONG).show();
if (user != null && user.isEmailVerified()) {
Intent myIntent = new Intent(PartyActivity.this, ChatActivity.class);
myIntent.putExtra("Other Uid", othersUid);
PartyActivity.this.startActivity(myIntent);
}
else {
//...
}
}
最佳答案
替换下一行
TextView otherUid = findViewById(R.id.uidTextView);
到:
TextView otherUid = view.findViewById(R.id.uidTextView);
它应该工作
关于java - 如何从列表中获取对象中的字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55398137/