Send Recycleview Data to Activity

Asked

Viewed 472 times

0

Well, I am new to Android development and I would like to know how to step the value of the option selected in Recycleview to an Edittext that is in another Activity

  • Try to formulate your question better, putting code than you have already done and where you are having difficulties. It makes it easier for someone to help you.

1 answer

1


There are several ways to do this. First you need to receive the click event RecyclerView in Activity and then you can pass this value on Intent to another Activity.

To detect the event click on an item from RecyclerView you can create a Listener within your ViewHolder and use an interface to communicate back data to Activity

Here is an example of the Adapter from RecyclerView

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
    private List<String> items;

    // Referência para o listener obtido no 'setOnClickListener'
    private OnItemClickListener clickListener;

    public ItemAdapter(List<String> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        // Aqui você infla o layout de cada view
        return new ViewHolder(null);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        String item = items.get(position);

        viewHolder.bind(item);
        // Aqui você faz o bind entre a view e o item da lista atual
    }

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


    // Método utilizado para comunicar o evento de clique de volta para a Activity
    public void setOnClickListener(OnItemClickListener onClickListener) {
        this.clickListener = onClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        String item;

        ViewHolder(@NonNull View itemView) {
            super(itemView);

            // Criando o evento de clique no item da lista e repassando o evento de volta para o listener
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickListener.onItemClick(item);
                }
            });
        }

        void bind(String item) {
            this.item = item;
        }
    }
}


// Interface utilizada para criar o contrato entre Adapter/Activity
interface OnItemClickListener {
    void onItemClick(String item);
}

And you would do it in Activity:

List<String> items = new ArrayList<>();

        ItemAdapter adapter = new ItemAdapter(items);

        adapter.setOnClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(String item) {
                // Agora você tem o item clicado e pode mandar ele para outra Activity

                Intent intent = new Intent(MainActivity.this, OutraActivity.this);
                intent.putExtra("item", item);
                startActivity(intent);
            }
        });

Another link to reference: https://stackoverflow.com/questions/24471109/recyclerview-onclick

Browser other questions tagged

You are not signed in. Login or sign up in order to post.