How to get row row id from ListView that contains clickable element?

Before I added an XML layout button for my rows, the row id was returned in the onListItemClick () callback when I clicked on a list item. Now that I have added a button to the list box layout, this callback no longer works. I read that this is normal. I was able to get text and a new button to refer to new callbacks by including these kinds of things in the XML layout file for my list line:

<Button
    android:onClick="newCallBackFunctionName"/>

The problem is that I cannot find a way to get the line identifier number corresponding to the list item in which the particular button was clicked. In the case of onListItemClick (), this was passed as part of the callback, but in the above case only the View object that clicked is returned back to the callback newCallBackFunctionName. What can I do about this?

* Edit: My list is populated with SimpleCursorAdaptor, if important.

* Edit: my lists and list of strings XML layouts look like this:

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>

+3
source share
2 answers

You can mark your buttons with the corresponding identifier or position in your adapter getView. For instance:

myButton.setTag(id);

onClick . :

public void newCallBackFunctionName(View v) {
    long id = (Long) v.getTag();
    // ...
}
+5

cursorAdapter, SimpleCursorAdapter, -

cursoradapter, getView, onclicklistener

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));        
}

, .

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
}
}
+2

All Articles