Events in Adapters listView

1

I am in the dilemma of having a listview, with a custom adapter

I have the following adapter

What I would like to achieve is that if you click on the sign to accept, or if you click on the sign to reject something happens,

The first thing I thought was to add an onclick to each image,

those icons are images, but it turns out that doing so, I do not know which of the items in the listView was clicked, I've researched and I have not found something that guides me to solve this problem, any help on how to solve this problem It's welcome, thank you very much

this is my adapter class

    <TextView
        android:layout_marginTop="5dp"
        android:id="@+id/txtNumberPhoneBilling"
        android:layout_width="168dp"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:text="textview"
        android:textSize="16dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_weight="0.67" />

<TextView
    android:id="@+id/txtMountBillingNotifications"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtNumberPhoneBilling"
    android:paddingLeft="20dp"
    android:text="TextViewss"
    android:textColor="@color/black"
    android:layout_gravity="center_vertical"
    android:textSize="16dp"/>

<TextView
    android:textColor="@color/black"

    android:id="@+id/txtDateBillingNotifications"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtMountBillingNotifications"
    android:text="TextView"
    android:textSize="16dp"
    android:paddingLeft="20dp"
    />


<ImageView
    android:id="@+id/btnAcceptBillingNotifications"
    android:layout_width="wrap_content"
    android:layout_height="25dp"
    android:layout_alignParentRight="true"
    android:paddingRight="40dp"
    android:layout_marginRight="100dp"
    android:background="@mipmap/aceptar"
    android:onClick="Acept"
    android:layout_centerInParent="true" />


<ImageView
    android:id="@+id/btnCancelBillingNotifications"
    android:layout_width="wrap_content"
    android:layout_height="25dp"
    android:layout_alignParentRight="true"
    android:paddingRight="40dp"
    android:layout_marginRight="50dp"
    android:background="@mipmap/cancelar"
    android:onClick="Cancel"
    android:layout_centerInParent="true" />

and this is my adapter class

package banred.twoinnovateit.com.Utils;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import banred.twoinnovateit.com.bimo.R;
/*
*Create by: Bruno Sosa
*Date: 17-10-2017
*Description: Clase de adapter del listado de cobros pendientes
*/

public class AdapterCobroPendiente extends BaseAdapter {

    protected Activity activity;
    protected ArrayList<CobroPendiente> items;

    public AdapterCobroPendiente(Activity activity, ArrayList<CobroPendiente> itemsCompra) {
        this.activity = activity;
        this.items = itemsCompra;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return Integer.parseInt(items.get(position).getNumber());
    }

    @Override
    public View getView(int position, View contentView, ViewGroup parent) {
        View vi = contentView;

        if (contentView == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            vi = inflater.inflate(R.layout.activity_billing_notifications_adapter, null);
        }

        CobroPendiente item = items.get(position);

        TextView number = (TextView) vi.findViewById(R.id.txtNumberPhoneBilling);
        number.setText(item.getNumber());

        TextView mount = (TextView) vi.findViewById(R.id.txtMountBillingNotifications);
        mount.setText(item.getMonto());

        TextView date = (TextView) vi.findViewById(R.id.txtDateBillingNotifications);
        date.setText(item.getFecha());

        return vi;
    }
}
    
asked by Bruno Sosa Fast Tag 23.10.2017 в 22:23
source

2 answers

1

It was important to add your Adapter since this is precisely where you can define a listener for each element of your ListView, for example here you can get the reference of ImageView with id btnAcceptBillingNotifications and btnCancelBillingNotifications a listener is assigned and you can see which button is clicked and which element in the list, in this example using a Toast.

      @Override
        public View getView(int position, View contentView, ViewGroup parent) {
            View vi = contentView;

            if (contentView == null) {
                LayoutInflater inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                vi = inflater.inflate(R.layout.activity_billing_notifications_adapter, null);
            }

            CobroPendiente item = items.get(position);

            TextView number = (TextView) vi.findViewById(R.id.txtNumberPhoneBilling);
            number.setText(item.getNumber());

            TextView mount = (TextView) vi.findViewById(R.id.txtMountBillingNotifications);
            mount.setText(item.getMonto());

            TextView date = (TextView) vi.findViewById(R.id.txtDateBillingNotifications);
            date.setText(item.getFecha());


 /*******************************/
 //Obtiene referencia de ImageView Aceptar.
 ImageView btnAcceptBillingNotifications = (ImageView)vi.findViewById(R.id.btnAcceptBillingNotifications);

 //Define Listener a ImageView.
    btnAcceptBillingNotifications.setOnClickListener(new View.OnClickListener{
            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "Aceptar de elemento " + position, Toast.LENGTH_LONG).show();
            }
    })

 //Obtiene referencia de ImageView Cancelar.
 ImageView btnCancelBillingNotifications= (ImageView)vi.findViewById(R.id.btnCancelBillingNotifications);

 //Define Listener a ImageView.
    btnAcceptBillingNotifications.setOnClickListener(new View.OnClickListener{
            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "Cancelar del elemento " + position, Toast.LENGTH_LONG).show();

            }
    })



 /*******************************/
            return vi;
        }
    
answered by 24.10.2017 / 17:13
source
1

Hello what you can do is send an interface to the adapter so that the one who creates the adapter knows what happens when an action is launched, something like this:

public interface BillingNotificationListener{

   void accepted(CobroPendiente cobroPendiente);

   void rejected(CobroPendiente cobroPendiente);
}

Then on your adapter you can do something like this:

protected Activity activity;
protected ArrayList<CobroPendiente> items;
protected BillingNotificationListener billingNotificationListener;

public AdapterCobroPendiente(Activity activity, ArrayList<CobroPendiente> itemsCompra, BillingNotificationListener billingNotificationListener) {
    this.activity = activity;
    this.items = itemsCompra;
    this.billingNotificationListener = billingNotificationListener;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int position) {
    return Integer.parseInt(items.get(position).getNumber());
}

@Override
public View getView(int position, View contentView, ViewGroup parent) {
    View vi = contentView;

    if (contentView == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi = inflater.inflate(R.layout.activity_billing_notifications_adapter, null);
    }

    final CobroPendiente item = items.get(position);

    TextView number = (TextView) vi.findViewById(R.id.txtNumberPhoneBilling);
    number.setText(item.getNumber());

    TextView mount = (TextView) vi.findViewById(R.id.txtMountBillingNotifications);
    mount.setText(item.getMonto());

    TextView date = (TextView) vi.findViewById(R.id.txtDateBillingNotifications);
    date.setText(item.getFecha());

    View btnAcceptBillingNotifications = vi.findViewById(R.id.btnAcceptBillingNotifications);

    btnAcceptBillingNotifications.setOnClickListener(new View.OnClickListener{
            @Override
            public void onClick(View v) {
                billingNotificationListener.accepted(item);
            }
    })

    return vi;
}
}

Greetings.

    
answered by 24.10.2017 в 15:21