3

Before I added a button to the layout XML for my rows, the row ID was returned in the callback onListItemClick() upon a list item being clicked. Now that I added a button to the list row layout, this callback doesn't work anymore. I read that this is normal. I have been able to get the text and the new button to refer to new callbacks via the inclusion of this sort of thing in the layout XML file for my list row:

<Button
    android:onClick="newCallBackFunctionName"/>

The problem is that I can't seem to find a way to retrieve the row id number corresponding to the list item in which the particular button that was pressed lies. In the case of onListItemClick() this was passed as part of the callback, but in the above case only the View object that is clicked is passed back to the callback newCallBackFunctionName. What can I do about this?

*Edit: My list is populated by a SimpleCursorAdaptor, in case that's important.

*Edit: My list and list row XML layouts are as follows:

List:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/basic_linear_layout_v1">

<TextView 
    android:id="@+id/category_selection_page_name"
    style="@style/page_heading_v1"/>

<ListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"/>

<TextView
    android:id="@id/android:empty"
    style="@style/problem_text_v1"
    android:text="@string/search_list_empty" />

<Button
    android:id="@+id/select_category_button"
    android:textSize="20sp"
    android:layout_weight="10"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:onClick="reload"/>

Row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView android:id="@+id/category_browse_name" style="@style/basic_list_item_v1"
    android:layout_weight="1"
    android:layout_gravity="left"
    android:focusable="true"
    android:clickable="true"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="right">
    <Button 
        android:text="..." 
        android:id="@+id/subcategories_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:focusable="true"
        android:onClick="onSubcategoryButtonClick">
    </Button>
</LinearLayout>

2 Answers2

5

You can tag your Buttons with the appropriate id or position in your adapter's getView method. For example:

myButton.setTag(id);

Then in your onClick handler, retrieve the tag from the view that was clicked. For example:

public void newCallBackFunctionName(View v) {
    long id = (Long) v.getTag();
    // ...
}
Jeff Gilfelt
  • 26,131
  • 7
  • 48
  • 47
  • For a CursorAdapter, you should set the tag in bindView, not getView. It's not much different, but it allows super.getView to do its View recycling. see http://stackoverflow.com/questions/3535074/getview-vs-bindview-in-a-custom-cursoradapter – John Price Jan 26 '17 at 19:39
2

you have to take little diff aproach you have to create your own cursorAdapter if it SimpleCursorAdapter then code will be some thing like this

first create a subclass of cursoradapter then override getView Method and then define onclicklistener for them

public class SMSimpleCursorAdapter extends SimpleCursorAdapter{

Context context;
Activity activity;
public SMSimpleCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.context=context;
    this.activity=(Activity) context;
}


@Override
public View getView(int position, View convertView, ViewGroup parent){
    View view = super.getView(position, convertView, parent);
    long id=getItemId(position);
    Button button = (Button)view.findViewById(R.id.btnDelete);
     //pass id in your handler
    button.setOnClickListener(new DeleteItemHandler(id, activity,this));        
}

and your handle class will be something like this

public class DeleteItemHandler implements OnClickListener,
        android.view.View.OnClickListener {
long id;
Activity activity;
SMSimpleCursorAdapter smSimpleCursorAdapter;
public DeleteItemHandler(long id, Activity activity,
        SMSimpleCursorAdapter smSimpleCursorAdapter) {
    super();
    this.id = id;
    this.activity = activity;
    this.smSimpleCursorAdapter = smSimpleCursorAdapter;
}
@Override
public void onClick(View v) {
          //your own item click code
}
}
Sunil Pandey
  • 7,042
  • 7
  • 35
  • 48
  • Thank you Sunil. Extending the SimpleCursorAdaptor worked well for me :) Your example is perhaps more complicated than I needed but I can see how it answers a more general case. Tagging UI elements with the row ID within my own implementation of getView, as Jeff answers here, has worked perfectly for me and only required about 5 lines of new code. – Sam Svenbjorgchristiensensen Mar 16 '11 at 11:12