Knickerless-Noggins Knickerless-Noggins - 2 years ago 224
Java Question

ListView row buttons: How do I create a custom Adapter that connects a View.OnClickListener to a button on each row of a ListView?

I want my ListView to contain buttons, but setting the button's xml property, onClick="myFunction" and then placing a public void myFunction(android.view.View view) method in the activity causes an NoSuchMethodException (the stack trace is null) to be thrown, as although the onclick listener is there, it doesn't fire myFunction(...) and cause the activity to close.

How do I create a custom Adapter that connects a View.OnClickListener to a button on each row of a ListView?

My ListView is created as follows...

[ content..]

public void myFunction(android.view.View view)
//Do stuff

[activity.xml content..]

<LinearLayout xmlns:tools="" xmlns:android="" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".FrmCustomerDetails" >
<ListView android:id="@+id/LstCustomerDetailsList" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:clickable="true" android:clipChildren="true" android:divider="@null" android:dividerHeight="0dp" android:fastScrollEnabled="true" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:requiresFadingEdge="vertical" android:smoothScrollbar="true" />

[activity_row_item.xml content..]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="" xmlns:tools="" android:id="@+id/Llt" android:layout_width="match_parent" android:layout_height="match_parent" >
<Button android:id="@+id/Btn" android:text="Click me" android:onClick="myFunction" />

Answer Source

Here is how to create the custom Adapter, connecting View.OnClickListener to a ListView with a button per row...

1. Create a layout for a typical row

In this case, the row is composed of three view components:

  • name (EditText)
  • value (EditText:inputType="numberDecimal")
  • delete (Button)


pay_list_item.xml layout is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:layout_height="fill_parent" >

        android:hint="Name" />

        android:text="0.0" />

        android:text="Remove Pay"
        android:onClick="removePayOnClickHandler" />


Note: the button has onClick handler defined in xml layout file, because we want to refer its action to a specific list item.

Doing this means that the handler will be implemented in Activity file and each button will know which list item it belongs to.

2. Create list item adapter

This is the java class that is the controller for pay_list_item.xml.

It keeps references for all of its views, and it also puts these references in tags, extending the ArrayAdapter interface.

The Adapter:

public class PayListAdapter extends ArrayAdapter<Payment> {

    private List<Payment> items;
    private int layoutResourceId;
    private Context context;

    public PayListAdapter(Context context, int layoutResourceId, List<Payment> items) {
        super(context, layoutResourceId, items);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.items = items;

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        PaymentHolder holder = null;

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new PaymentHolder();
        holder.Payment = items.get(position);
        holder.removePaymentButton = (ImageButton)row.findViewById(;
        holder.removePaymentButton.setTag(holder.Payment); = (TextView)row.findViewById(;
        holder.value = (TextView)row.findViewById(;


        return row;

    private void setupItem(PaymentHolder holder) {;

    public static class PaymentHolder {
        Payment Payment;
        TextView name;
        TextView value;
        ImageButton removePaymentButton;

Here we list the Payment class items.

There are three most important elements here:

  • PayListAdapter constructor: sets some private fields and calls superclass constructor. It also gets the List of Payment objects. Its implementation is obligatory.
  • PaymentHolder: static class that holds references to all views that I have to set in this list item. I also keep the Payment object that references to this particular item in list. I set it as tag for ImageButton, that will help me to find the Payment item on list, that user wanted to remove
  • Overriden getView method: called by superclass. Its goal is to return the single List row. We create its fields and setup their values and store them in static holder. Holder then is put in row’s tag element. Note that there is a performance issue, as the row is being recreated each time it is displayed. I used to add some flag in holder like isCreated, and set it to true after row was already created. then you can add if statement and read tag’s holder instead of creating it from scratch. is quite simple as for now and it looks a bit like BasicNameValuePair:

public class Payment implements Serializable {
    private String name = "";
    private double value = 0;

    public Payment(String name, double value) {

There are additional gets and sets for each private field not shown.

3. Add ListView to the activity layout xml file

In its simpliest form, it will be enough to add this view to activity layout:


4. Set up adapter to this list view in Activity Java code

In order to display items in ListView you need to set up its adapter and map it to some other ArrayList of Payment objects (as I am extending an Array adapter here). Here is code that is responsible for binding adapter to editPersonData.getPayments() ArrayList:

PayListAdapter adapter = new PayListAdapter(AddNewPerson.this, R.layout.pay_list_item, editPersonData.getPayments());
ListView PaysListView = (ListView)findViewById(;

5. Adding / removing items to ListView (and its adapter)

Adapter is handled just like any other ArrayList, so adding new element to it is as simple as:

Payment testPayment = new Payment("Test", 13);

6. Handle Remove Payment button click event

In an activity’s code, where ListView is displayed, add public method that will handle remove button click action. The method name has to be exactly the same as it was in pay_list_item.xml:

The method body is as follows:

public void removePayOnClickHandler(View v) {
    Payment itemToRemove = (Payment)v.getTag();

The Payment object was stored in ImageButton’s Tag element. Now it is enough to read it from Tag, and remove this item from the adapter.

7. Incorporate remove confirmation dialog window

Probably you need also make sure that user intentionally pressed the remove button by asking him additional question in confirmation dialog.


a) Create dialog’s id constant

This is simply dialog’s ID. it should be unique among any other dialog window that is handled by current activity. I set it like that:

protected static final int DIALOG_REMOVE_CALC = 1;
protected static final int DIALOG_REMOVE_PERSON = 2;

b) Build dialog

I use this method to build dialog window:

private Dialog createDialogRemoveConfirm(final int dialogRemove) {
    return new AlertDialog.Builder(getApplicationContext())
    .setPositiveButton(R.string.calculation_dialog_button_ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
    .setNegativeButton(R.string.calculation_dialog_button_cancel, null)

AlertDialog builder pattern is utilized here. I do not handle NegativeButton click action – by default the dialog is just being hidden. If dialog’s confirm button is clicked, my handleRemoveConfirm callback is called and action is performed based on dialog’s ID:

protected void handleRemoveConfirm(int dialogType) {
    if(dialogType == DIALOG_REMOVE_PERSON){
    }else if(dialogType == DIALOG_REMOVE_CALC){

c) Show Dialog

I show dialog after my remove button click. The showDialog(int) is Android’s Activity’s method:

OnClickListener removeCalcButtonClickListener = new OnClickListener() {
    public void onClick(View v) {

the showDialog(int) method calls onCreateDialog (also defined in Activity’s class). Override it and tell your app what to do if the showDialog was requested:

protected Dialog onCreateDialog(int id) {
    switch (id) {
        return createDialogRemoveConfirm(DIALOG_REMOVE_CALC);
        return createDialogRemoveConfirm(DIALOG_REMOVE_PERSON);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download