Sam Justman
Sam Justman

Reputation: 412

How to get a String value in an Object from a List?

I want to get a String value from an Object that consists of an ImageView and two TextView in my List by clicking on that specific Object. But the problem is, I cannot get the String that I want.

The toast message should show the value of second TextView but it shows whatever it likes.

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" />

If you have any other suggestion that helps me to get any Object specific value from clicked Object, I would like to hear that.

Thank you very much in advance.

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 {
        //...
    }
}

Upvotes: 1

Views: 89

Answers (2)

TomD88
TomD88

Reputation: 751

... Hi Sam, You need to set the text of the second TextView before getting it. You can do this adding android:text to the second textview in the xml to have static text (the default value). To have dynamic text into your second textview just set the text when you are creating the element of the List. Hope this help.

Cheers

Upvotes: 0

Saurav Kumar
Saurav Kumar

Reputation: 970

Replace below line

TextView otherUid = findViewById(R.id.uidTextView);

to:

TextView otherUid = view.findViewById(R.id.uidTextView);

It should work

Upvotes: 1

Related Questions