'How can data be stored in the Recycler View?
I've tried to read articles and pages about adaptors, but I couldn't really understand it. So I followed a Youtube tutorial that works perfectly fine, even though I don't know why. If you could explain me in simple words like you would do in a pseudocode or something, it would be perfect. What I really want to know is how this adaptor is able to get the data and place it in different text boxes each time a new register is added. The code I adapted from the video is below and if you'd like to watch it yourself, this is the link: https://www.youtube.com/watch?v=VQKq9RHMS_0 (I'm new here, sorry if I've done something wrong)
public class Adaptador extends RecyclerView.Adapter<Adaptador.MyViewHolder> {
private Context context;
private Activity activity;
private ArrayList purch_id, purch_loja, purch_produto, purch_valor, purch_data, purch_meiopagamento, purch_descricao;
Adaptador(Activity activity, Context context, ArrayList purch_id,
ArrayList purch_loja,
ArrayList purch_produto,
ArrayList purch_valor,
ArrayList purch_data,
ArrayList purch_meiopagamento,
ArrayList purch_descricao){
this.activity = activity;
this.context = context;
this.purch_id = purch_id;
this.purch_loja = purch_loja;
this.purch_produto = purch_produto;
this.purch_valor = purch_valor;
this.purch_data = purch_data;
this.purch_meiopagamento = purch_meiopagamento;
this.purch_descricao = purch_descricao;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.recycler_layout, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.txtpurch_id.setText(String.valueOf(purch_id.get(position)));
holder.txtpurch_loja.setText(String.valueOf(purch_loja.get(position)));
holder.txtpurch_produto.setText(String.valueOf(purch_produto.get(position)));
holder.txtpurch_valor.setText(String.valueOf(purch_valor.get(position)));
holder.txtpurch_meiopagamento.setText(String.valueOf(purch_meiopagamento.get(position)));
holder.txtpurch_data.setText(String.valueOf(purch_data.get(position)));
holder.txtpurch_descricao.setText(String.valueOf(purch_descricao.get(position)));
}
@Override
public int getItemCount() {
return purch_id.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView txtpurch_id, txtpurch_loja, txtpurch_produto,txtpurch_valor,txtpurch_meiopagamento,
txtpurch_data, txtpurch_descricao;
MyViewHolder(@NonNull View itemView) {
super(itemView);
txtpurch_id = itemView.findViewById(R.id.txtpurch_id);
txtpurch_loja = itemView.findViewById(R.id.txtpurch_loja);
txtpurch_produto = itemView.findViewById(R.id.txtpurch_produto);
txtpurch_valor = itemView.findViewById(R.id.txtpurch_valor);
txtpurch_meiopagamento = itemView.findViewById(R.id.txtpurch_meiopagamento);
txtpurch_data = itemView.findViewById(R.id.txtpurch_data);
txtpurch_descricao = itemView.findViewById(R.id.txtpurch_descricao);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
