简体   繁体   English

Android ListView:在不使用点击事件的情况下获取项目文本

[英]Android ListView: Getting text of item without using click event

Is there a way to get the value of TextView which is an item of a ListView without using any click event ?有没有办法在不使用任何点击事件的情况下获取作为 ListView 项目的TextView的值?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/friendNoTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/friendImage"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/friendFullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/friendImage"
        android:textColor="#0D47A1" />

    <ImageButton
        android:id="@+id/addFriendButton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/add_friend" />

    <TextView
        android:id="@+id/addFriendTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/addFriendButton"
        android:layout_marginTop="5dp"
        android:text="ADD FRIEND"
        android:textColor="#0D47A1"
        android:textSize="10sp" />

</RelativeLayout>

My custom row layout defined as above and i need to get the value of the friendNoTv TextView without any click event.我的自定义行布局定义如上,我需要在没有任何点击事件的情况下获取friendNoTv TextView的值。 I tried it as我试过了

@Override
public View getView(int index, final View convertView, ViewGroup parent) {

    // check if we need to generate a new View from scratch or recycle one
    View InflatedView = convertView;
    if (InflatedView == null) {
        InflatedView = Inflater.inflate(R.layout.find_friends_result_row, parent, false);
    }

    // setup variables

    final TextView friendNo = (TextView) InflatedView.findViewById(R.id.friendNoTv);
    ImageView friendImage = (ImageView) InflatedView.findViewById(R.id.friendImage);
    TextView friendFullName = (TextView) InflatedView.findViewById(R.id.friendFullName);
    ImageButton addFriendButton = (ImageButton) InflatedView.findViewById(R.id.addFriendButton);

    //Exception here, friendNo.getText().toString() comes up NULL
    final int friendId = Integer.parseInt(friendNo.getText().toString());

    if(isFriend(friendId)){
        addFriendButton.setVisibility(View.GONE);
    }else{
        addFriendButton.setVisibility(View.VISIBLE);
    }

    //TODO

    Friend friend = friendList.get(index);

    DataFormatter dataFormatter = new DataFormatter();

    friendNo.setText(Integer.toString(friend.getBorNo()));
    dataFormatter.decodeimage(friend.getUserImage(),friendImage);
    friendFullName.setText(friend.getFullName());

    return InflatedView;

}

But value comes up Null which causes an Exception .但是 value 出现Null导致Exception How do i fix this ?我该如何解决 ? Any help would be appreiated.任何帮助都会受到赞赏。

Remove android:visibility="gone" from删除 android:visibility="gone" 从

<TextView
    android:id="@+id/friendNoTv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" />

and also add text to above text view并将文本添加到上面的文本视图

for example例如

android:text="1"

You are getting null because you just have inflated the view and you are trying to get the text before setting it.你得到 null 因为你刚刚膨胀了视图并且你试图在设置它之前获取文本。 In your XML you haven't set any text value that's why you are getting nothing there.在您的 XML 中,您没有设置任何文本值,这就是为什么在那里什么也没有。

Just replace that line which is giving null with below one,只需将给出 null 的那一行替换为下面的一行,

 final int friendId = Integer.parseInt(friendList.get(index).getFriendId.toString());

Your code here:你的代码在这里:

//Exception here, friendNo.getText().toString() comes up NULL
 final int friendId = Integer.parseInt(friendNo.getText().toString());

will definitely be null as friendNo is not set yet.肯定会为空,因为friendNo 尚未设置。 you should paste these line at last just before return statement with the conditional statement.您应该最后将这些行粘贴在带有条件语句的 return 语句之前。

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

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