So far i have a list view with a custom adapter, and each item in list has a button.
Im really confused; Im trying to do the following:

When user clicks on button(a delete button) in item in the list, i want to know in which item button was clicked so i can know which item to delete-how do i implement this? Ive seen something about setting tags, but Im still very lost.

I have also tried to reach the button from the list layout from my main activity, and cannot reference it.

please can you give me a detailed description on how to do what i want to do thanks.

**ADDED adapter code:**

```
public class LocationAdapter extends BaseAdapter{
String [] n;
Context context;
String[] a;

private static LayoutInflater inflater=null;
public LocationAdapter(MainActivity mainActivity, String[] names, String[] addresses) {
    // TODO Auto-generated constructor stub
    n=names;
    context=mainActivity;
    a=addresses;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return n.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView name;
    TextView address;
    ImageButton ib;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.rowlayout2, null);
    holder.name =(TextView) rowView.findViewById(R.id.EditTextName);
    holder.address =(TextView) rowView.findViewById(R.id.EditTextAddress);
    holder.ib = (ImageButton) rowView.findViewById(R.id.Delete);
    holder.name.setText(n[position]);
    holder.address.setText(a[position]);
    holder.ib.setTag(convertView);
    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+n[position], Toast.LENGTH_LONG).show();
        }

    });
    return rowView;
}

```

}
['https://Stackoverflow.com/questions/29813126', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4725548/']
Implement an `OnClickListener` for your `delete` button.

When the `delete` button is clicked, remove the row in the data source at `position`, and then call `notifyDataSetChanged()`

```
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.rowlayout2, null);
    holder.name =(TextView) rowView.findViewById(R.id.EditTextName);
    holder.address =(TextView) rowView.findViewById(R.id.EditTextAddress);
    holder.ib = (ImageButton) rowView.findViewById(R.id.Delete);
    holder.name.setText(n[position]);
    holder.address.setText(a[position]);
    holder.ib.setTag(convertView);

    //Add this for on-click of delete button
    holder.ib.setOnClickListener(new OnClickListener(){
         //Delete the row in your data source specified at position

    });

    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+n[position], Toast.LENGTH_LONG).show();
        }

    });
    return rowView;
}

```
Use this inside your getView() method of adapter.

```
    yourButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
               System.out.println("button : "+position+ " clicked");
               // Do you code here.
        }
    });

```