I use ListView to show several items. My row.xml as below:
<TextView android:text="text"
android:id="@+id/tvViewRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button android:text="Click me!"
android:id="@+id/BtnToClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClick">
</Button>
And I define myClick in Activity as below:
public void myClick (View v) {
LinearLayout vwParentRow = (LinearLayout)v.getParent();
//How to get the position
}
How to know the position which buttom be clicked? The position mean same as the method onListItemClick's.
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
Try
You can try like this.
Step 1: In your custom adapter
Step 2: In onclick listener
If I understand your question correctly, you have a button in each row of a
ListView
, and you want to know which row received the button click. How are you doing ansetOnClickListener()
on the button? The reason I ask this is - if you are setting theOnClickListener
for each button, you already know the position of that button.You should also read the documentation for ListView and even look through the available methods and their sample code.
Know your documentation.
Yes the position in the onListItemClick is same as the position of the item clicked in the list.