getting position of click and custom button inside a list using custom CursorAdapter

android, android-cursoradapter, android-listview

Solution

The simple way to handle your scenario is to leverage the button tag

in your bindView

ImageButton button = (ImageButton) view.findViewById(R.id.button);
button.setTag(cursor.getPosition());

and then in your onClick you can read the tag

int position = (Integer) view.getTag();

Problem

Scenario Friends I have a ListView which I am using in a FrameLayout which will power my DrawerLayout for the sliding menu. I have custom buttons and text in my ListView which are defined as listitems in seperate xml. Now I am providing data to this ListView using a customAdapter which is extends CursorAdapter... I want to get the position of the click and also the button click events to fetch data accordingly... my code below... List item XML ``` <?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:descendantFocusability="blocksDescendants" > <!-- ToggleButton android:id="@+id/button" android:layout_width="30dp" android:layout_height="30dp" android:layout_margin="10dp" style="@style/toggleButton" android:background="@drawable/silent_button_selector" android:focusable="false" /> --> <ImageButton android:id="@+id/button" android:layout_width="50dp" android:layout_height="50dp" android:layout_margin="10dp" android:src="@drawable/icon_button" /> <TextView android:id="@+id/calendarName" android:layout_width="wrap_content" android:layout_margin="10dp" android:layout_height="wrap_content" android:textSize="25sp" android:layout_toRightOf="@+id/button" /> </RelativeLayout> ``` Adapter class: ``` public class CalendarListAdapter extends CursorAdapter implements OnClickListener{ @SuppressWarnings("deprecation") public CalendarListAdapter(Context context, Cursor c) { super(context, c); // TODO Auto-generated constructor stub } @Override public void bindView(View view, Context context, Cursor cursor) { // TODO Auto-generated method stub TextView title = (TextView) view.findViewById(R.id.calendarName); System.out.println("cursor details" + cursor.getString(cursor.getColumnIndex(cursor .getColumnName(1)))); title.setText(cursor.getString(cursor.getColumnIndex(cursor .getColumnName(1)))); Log.d("click position " , "position is " +cursor.getPosition() ); ImageButton button = (ImageButton) view.findViewById(R.id.button); button.setOnClickListener(this); Log.d("click position " , "position is " +cursor.getPosition() ); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater inflater = (LayoutInflater) context .getSystemService(context.LAYOUT_INFLATER_SERVICE); View calendarListView = inflater.inflate(R.layout.calendar_list_item, null); return calendarListView; } @Override public void onClick(View view) { // TODO Auto-generated method stub Log.d("item clicked", "clicked" ); } } ``` What is happening now : with the above implementation, when I click button it give only button click events and when I click rows/list items I get the position alone. What I need to do? I need to get the position and the click event simultaneously so when I click button on 3rd row, i need position 3 and button clicked event... Please let me know the best way to do this... Thanks for your time and efforts..

Original source