Well, I'm new to Android development and would like to know how to pass the value of the selected option in Recycleview to an EditText that is in another Activity
Well, I'm new to Android development and would like to know how to pass the value of the selected option in Recycleview to an EditText that is in another Activity
There are several ways to do this. First you need to get the click event on RecyclerView
on Activity
current and then you can pass that value on Intent
to another Activity
.
To detect the click event on a RecyclerView
item you can create a listener within your ViewHolder
and use an interface to communicate back the data to the Activity
Here is an example of the RecyclerView
adapter
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 this on 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 for reference: link