how to get a list item position by clicking the button inside it? how to get a list item position by clicking the button inside it? android android

how to get a list item position by clicking the button inside it?


Actually, I used the same approach - just added casting the parent layout and I got the position w/o any exception

public void deleteButtonClick(View v) {        //TODO Remove favorite - DB + file system        Toast.makeText(this, "Deleting bookmark", Toast.LENGTH_SHORT).show();        final int position = getListView().getPositionForView((LinearLayout)v.getParent());        if (position >= 0) {            Favorite o = (Favorite) this.getListAdapter().getItem(position);        }}

My layout for listview row :

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res/com.bbox.application"    android:orientation="vertical" android:layout_width="fill_parent"    android:layout_height="fill_parent">    <com.markupartist.android.widget.ActionBar        android:id="@+id/actionbar" app:title="@string/ac_title" style="@style/ActionBar" />    <ListView android:id="@+id/android:list" android:layout_width="fill_parent"        android:layout_height="wrap_content" android:layout_weight="5"        android:background="@drawable/categrory_bckgr" />    <TextView android:id="@+id/android:empty"        android:layout_width="fill_parent" android:layout_height="wrap_content"        android:text="@string/main_no_items" /></LinearLayout>

I hope it'll help.


An easier solution to this probably is when your Activity implements OnClickListener and you set the (casted) Context as OnClickListener to any view you want in the Adapter. For more robust code you can check with instanceof.

public class MyActivity implements OnClickListener {    // ...}public class MyAdapter extends CursorAdapter {    @Override    public void bindView(View view, Context context, Cursor cursor) {        final TextView tv = (View) view.findViewById(R.id.text1);        setOnClickListener((OnClickListener)context);        tv.setTag(cursor.getPosition());    }}

For convenience you can set the items position in the adapter as Tag of the view. That way you can easily lookup the whole item in the adapter.


We can get the position by implementing onClickListener inside the getView method of adapter.

ADAPTER :

public class DetailsListAdapter extends BaseAdapter {Context context;ArrayList<Profile> data;public DetailsListAdapter(Context context, ArrayList<Profile> data) {    this.context = context;    this.data = data;}@Overridepublic int getCount() {    return data.size();}@Overridepublic Object getItem(int position) {    return data.get(position);}@Overridepublic long getItemId(int position) {    return position;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {    ViewHolder viewHolder;    if (convertView == null) {        convertView = LayoutInflater.from(context).inflate(R.layout.list_item_logs_details, null);        viewHolder = new ViewHolder();        viewHolder.btn = (Button) convertView.findViewById(R.id.log_details_1_a);        viewHolder.btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                    Log.d("statusPosition","position "+position);            }        });        convertView.setTag(viewHolder);    } else {        viewHolder = (ViewHolder) convertView.getTag();    }    return convertView;}public static class ViewHolder {    TextView tv2, tv3, tv3a, tv4, tv4a;    Button btn;}}