odr_m9611
odr_m9611

Reputation: 303

Get text of TextView from a row of ListView

I have a layout for a row of LIstView items like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:descendantFocusability="afterDescendants"
    android:gravity="right"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:textSize="40sp" 
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"  />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/btn_background" >

        <Button
            android:id="@+id/share_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="2dp"
            android:background="@drawable/delete_btn"
            android:drawableRight="@drawable/delete"
            android:paddingBottom="3dp"
            android:paddingTop="2dp"
            android:text="share"
            android:onClick="myClickHandler" />

    </RelativeLayout>

</LinearLayout>

And i want to get the text of TextView when user click on delete button(in myClickHandler() method),

with this code:

RelativeLayout vwParentRow = (RelativeLayout)v.getParent();

I get the parent of Button(RelativeLayout), but don't know how to get the text of TextView that in parent of parent of Button

How i can to do this?

Thanks

Upvotes: 0

Views: 3682

Answers (4)

Satheesh
Satheesh

Reputation: 1730

It should working

  TextView textviewDate=(TextView)findViewById(R.id.label);
  String selectedText=textviewDate.getText().toString();

Upvotes: 0

koleanu
koleanu

Reputation: 495

TextView yourTextView = (TextView )v.getParent().getParent().findViewById(R.id.label)
String yourText = yourTextView.getText().toString(); 

Upvotes: 0

Arun C
Arun C

Reputation: 9035

Try this in myClickHandler() method

RelativeLayout vwParentRow = (RelativeLayout)v.getParent();
LinearLayout vwGrandParentRow = (LinearLayout)vwParentRow.getParent();

TextView yourTV=(TextView)vwGrandParentRow.findViewByid(R.id.label);

Log.i("Text",yourTV.getText().toString());

Upvotes: 3

Sushil
Sushil

Reputation: 8478

You can use TextView.getText().toString()

Upvotes: 0

Related Questions