1

Me encuentro en el dilema de tener un listview , con un adapter personalizado

tengo el siguiente adapter

introducir la descripción de la imagen aquí

lo que quisiera lograr es que si clikcea en el signo de aceptar , o si clickea en el signo de rechazar suceda algo ,

lo primero que pense es agregarl un onclick a cada imagen ,

esos iconos son imagenes , pero resulta que haciendolo asi , no sabre cual de todos los items del listView fue el clickedo , eh investigado y no eh encontrado algo que me guie a resolver dicho problema , cualquier ayuda de como poder solucionar este problema es bienvenida , desde ya gracias

esta es mi clase adapter

    <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" />

y esta es mi clase de adapter

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;
    }
}
4
  • Bruno, agrega tu Adapter para que sea mejor tu pregunta.
    – Jorgesys
    el 23 oct. 2017 a las 21:03
  • Ya esta editado el 24 oct. 2017 a las 13:03
  • Prueba a desarrollar solo un onClick y dentro metes un if y else if mirando si el evento que le llega es igual al findById (R.res.'lo que quieras') el 24 oct. 2017 a las 13:11
  • no te entendi muy buen DazzaekWazzel el 24 oct. 2017 a las 13:14

2 respuestas 2

1

Era importante agregaras tu Adapter ya que precisamente aquí es donde puedes definir un listener para cada elemento de tu ListView, por ejemplo aquí puedes obtener la referencia de los ImageView con id btnAcceptBillingNotifications y btnCancelBillingNotifications se asigna un listener y puedes ver que botón se da click y de que elemento en el listado, en este ejemplo mediante un 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;
        }
1

Hola lo que puedes hacer es enviar una interface al adapter para que el que crea el adapter sepa que pasa cuando se lanza alguna acción, algo así:

public interface BillingNotificationListener{

   void accepted(CobroPendiente cobroPendiente);

   void rejected(CobroPendiente cobroPendiente);
}

Luego en tu adapter puedes hacer algo así:

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;
}
}

Saludos.

1

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.