Richard Richard - 1 year ago 71
Android Question

Passing a parameter to onclick listener on a button in a listview row

I just started android and i'm running into some problems.

I have created a listview that is populated from a database.

Each row has a button to delete the item from the list and the database.

I am able to hook up an event listener to the button but I have not been able to determine the matching database record to be deleted.

My class is below

public class FeedArrayAdapter extends ArrayAdapter<Feed> implements OnClickListener
private ARssEReaderDBAdapter dba;
private String TAG = "FeedArrayAdapter";

public FeedArrayAdapter(Context context, int textViewResourceId, List<Feed> items) {
super(context, textViewResourceId, items);
Log.w(TAG, "List");

public View getView(int position, View convertView, ViewGroup parent){
Log.w(TAG, "getView");
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.feedlistrow, null);
Feed feed = getItem(position);
if (feed != null){
TextView title = (TextView)v.findViewById(;
if (title != null){
Button btnDelete = (Button)v.findViewById(;
btnDelete.setOnClickListener(this); //btnDeleteFeedListener
return v;

public void onClick(View v){
Log.w(TAG, "something got clicked: ");


So how can I pass the database record ID to the handler so I can use the database adapter to delete it?

Answer Source

You should store the ID of the record in the Tag, call setTag() on your view, and to read in onclick call getTag()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download