java - 如何从列表中获取对象中的字符串值?

标签 java android string

我想从一个对象中获取一个字符串值,该对象由列表中的一个 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/

相关文章:

javascript - 正则表达式中的括号未被替换

java - 根据条件计算项目总数

java - 如何改变对 JTable 的关注?

Android setHorizo​​ntalFadingEdgeEnabled 不适用于三星 Galaxy S3 GT-I9300

string - 管理 CStringArray 条目

regex - 何时使用正则表达式与内置字符串方法?

java - ClassReader 中的 ArrayIndexOutOfBoundsException 导致加载 ApplicationContext 失败

java - JFreeChart 将数组加载到数据集中

android - getTargetFragment() 返回 null —— 在一个地方有效,但在另一个地方无效

java - 共享首选项无法正确保存