Return onClick value of an Adapter - Android Studio

0

Hi, I have a question about how to return a value to my Main activity

In my Adapter when clicking on a carview if I get the value and I show it a Toast but now I want to return that value to my main activity ... any idea of how to do it! ??

 public void onBindViewHolder(AdapterHolder holder, final int position) {
    final Letra letra = listletras.get(position);
    holder.nombre.setText(listletras.get(position).getNombre());
    holder.imagen.setImageResource(R.drawable.buss);
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "ELEMENTO " + letra.getId(), Toast.LENGTH_SHORT).show();


        }
    });
} 
    
asked by Erick Sánchez Chávez 24.09.2018 в 03:49
source

1 answer

0

To do this you should implement an onClick interface on your adapter, and then on your MainActivity call it, so we can know what item you want from the list and you can do what you need inside your class.

first in the adapter we define the interface

/**Creamos una interfaz que va a definir el listener del click*/
    public interface ListItemClickListener{
        void onListItemClick(int clickedItemIndex);
    }

Then we declare that click as global and we pass it to the constructor of our adapter

/** Creamos el Listener que va a escuchar por la posicion a clicar en la lista */
    final private ListItemClickListener mOnClickListener;

This is the constructor where we initialize it

public RecyclerAdapter(ListItemClickListener listener){
 //Le decimos al adaptador que item clicamos
mOnClickListener = listener;
}

In our viewHolder we tell you which element we are going to click to return a value and we pass the implementation of the interface of onClickListener

 class NumberViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView listItemNumberView;

        public NumberViewHolder(View itemView) {
            super(itemView);

            //Encontramos el textview que creamos en el layout
            listItemNumberView = (TextView) itemView.findViewById(R.id.tv_item_number);
            //Le decimos que se utilize clickListener en este constructor
            itemView.setOnClickListener(this);

        }

Inside the viewHolder will ask us to implement the methods of the interface, in this case the onClick, we add it

 @Override
        public void onClick(View view) {
            //Obtenemos la posicion en el adaptador que clicamos
            int clickedPosition = getAdapterPosition();
            //Al final le pasamos al click listener la posicion que acabamos de clicar
            mOnClickListener.onListItemClick(clickedPosition);


        }

Now finally we have to call this ListItemClickListener that we created in the adapter and assign it in our MainActivity

We're going to MainActivity.class

We implemented our click interface

public class MainActivity extends AppCompatActivity implements RecyclerAdapter.ListItemClickListener...

We put the methods of the interface, in this case the onListItemClick

@Override
    public void onListItemClick(int clickedItemIndex) {

        //Con esto chequeamos que no se vayan encolando los textView y al clicar en distintos items solo nos muestre el que clicamos
        if(mToast!= null){
            mToast.cancel();
        }
        String toastMessage = "Item #" + clickedItemIndex + " clicked";
        mToast = Toast.makeText(this,toastMessage,Toast.LENGTH_LONG);
        mToast.show();

    }

This example only shows one toast for each element clicked, but here you can do anything with the click of each element, put putExtras and more.

I have done a tutorial on how to use RecyclerView with this function: Link

And the github repo so you have the code: Link

    
answered by 24.09.2018 в 14:56