有沒有辦法獲得值爲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
。我該如何解決 ?任何幫助將被理解。
請張貼代碼有關創建更新@PierGiorgioMisley –
的,你打電話的價值,而不是適配器:) – Tartar
我的意思是在你創建列表和活動代碼列表 –