[英]Getting Row position in android listview
嗨,這聽起來像是一個愚蠢的問題,但它困擾了我幾天,而且我還沒有在互聯網上找到答案。 在不覆蓋onItemclick方法的android Listview中,還有其他任何方法可以獲取行的位置。
我使用setTag()/ getTag()方法獲取項目的位置。 在列表適配器的getView()方法中,使用setTag(“ position”)方法將項目的位置設置為行項目的標簽(在您的情況下為按鈕)。
單擊任意行中的按鈕時,將為該按鈕調用onClick()方法,在onClick()方法中,您可以使用getTag()方法獲取位置。 對所有行按鈕使用與onClick例程相同的方法。
示例代碼:列表項可能是這樣的
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:background="@drawable/grad"
android:onClick="onButtonClick" />
getView()方法可能是這樣的:
public View getView (int position, View convertView, ViewGroup parent) {
...
...
button.setTag(position);
...
}
onClick例程可能是這樣的:
public void onButtonClick(View v) {
String tag = v.getTag().toString();
if(tag != null) {
int position = Integer.parseInt(tag);
}
...
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.