2015-08-28 76 views
-3

有沒有辦法獲得值爲TextView這是ListView的一個項目,而不使用任何點擊事件?Android ListView:不使用點擊事件獲取項目的文本

<?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> 

我的自定義行佈局定義如上,我需要得到friendNoTv TextView的價值沒有任何點擊事件。我想它作爲

@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; 

} 

但值出現Null這導致Exception。我該如何解決 ?任何幫助將被理解。

+0

請張貼代碼有關創建更新@PierGiorgioMisley –

+0

的,你打電話的價值,而不是適配器:) – Tartar

+0

我的意思是在你創建列表和活動代碼列表 –

回答

-1

刪除安卓知名度=「水漲船高」,從

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

,並添加文本上述文本視圖

例如

android:text="1" 
-1

你得到空,因爲你已經膨脹該視圖和您正在設置之前嘗試獲取文本。在你的XML中,你沒有設置任何文本值,這就是爲什麼你沒有在那裏得到什麼。

只需更換該行即給予空地下1,

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

-1

您的代碼在這裏:

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

肯定會null作爲friendNo尚未設定。您應該在條件語句返回語句之前最後粘貼這些行。

相關問題