简体   繁体   English

从ListView的一行获取TextView的文本

[英]Get text of TextView from a row of ListView

I have a layout for a row of LIstView items like this: 我有一行这样的LIstView项的布局:

<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), 我想当用户单击删除按钮(在myClickHandler()方法中)时获取TextView的文本,

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 我得到了Button(RelativeLayout)的父级,但是不知道如何获取Button的父级中TextView的文本

How i can to do this? 我该怎么做?

Thanks 谢谢

Try this in myClickHandler() method myClickHandler()方法中尝试

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

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

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

您可以使用TextView.getText()。toString()

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

It should working 它应该工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM