How to pass data received from an activity and put it in a recyclerview

0

Hello, I'm a beginner in java programming and I'm having trouble passing some serialized information from an activity to a recyclerview.

When the second activity starts, it does not contain any information. Before trying to put in the recyclerview I tried using Textview and it worked, however when replacing for a recycler it does not bring any information. I think it's simple, but since I'm a beginner, I have no idea.

Below is the project code.

Thanks in advance for the help.

MainActivity

private void onOrderProduct() {
    bOrder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Cart cart = new Cart();
            cart.setComida(tvTitle.getText().toString());
            cart.setPreco(tvTotal.getText().toString());
            cart.setQuantidade(tvQtd.getText().toString());

            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("cart", cart);
            startActivity(intent);
        }
    });
}

SecondActivity

public class SecondActivity extends AppCompatActivity {
RecyclerView rvCart;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondactivity);

    rvCart = (RecyclerView) findViewById(R.id.tela5recycler_view);
    rvCart.setHasFixedSize(true);

    LinearLayoutManager manager = new LinearLayoutManager(this);

    rvCart.setLayoutManager(manager);


    if (getIntent().getSerializableExtra("cart") != null) {


        Intent intent = getIntent();

        Cart cart = (Cart) intent.getSerializableExtra("cart");

        ArrayList<Cart> eList = new ArrayList<>() cart;
        Adapter adapter =new Adapter(getApplicationContext(), eList );
        rvCart.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

}
}

Adapter

public class Adapter extends RecyclerView.Adapter<Adapter.ItemViewHolder> {

private Context context;
private ArrayList<Cart> itemList;

public Adapter(Context context, ArrayList<Cart> itemList){
    this.context = context;
    this.itemList = itemList;
}

@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.from(parent.getContext())
            .inflate(R.layout.adapter_card, parent, false);

    ItemViewHolder itemViewHolder = new ItemViewHolder(view);
    return itemViewHolder;
}

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {

    Cart item = itemList.get(position);

    holder.tvQtdcard.setText(item.getQuantidade());
    holder.tvComidacard.setText(item.getComida());
    holder.tvPrecocard.setText(item.getPreco());

}



@Override
public int getItemCount() {
    if(itemList != null){
        return itemList.size();
    }
    return 0;
}
public static class ItemViewHolder extends RecyclerView.ViewHolder{

    public CardView cvItem;
    public TextView tvQtdcard, tvComidacard, tvPrecocard;

    public ItemViewHolder(View itemView) {
        super(itemView);
        cvItem = (CardView)itemView.findViewById(R.id.tela1_1_1_1_1card);
        tvQtdcard = (TextView)itemView.findViewById(R.id.tela5qtdcard);
        tvComidacard = (TextView)itemView.findViewById(R.id.tela5comidacard);
        tvPrecocard = (TextView)itemView.findViewById(R.id.tela5precocard);


    }
}
}

Serializable Cart.class

import java.io.Serializable;



public class Cart implements Serializable{

private static final long serialVersionUID = 42L;
private String comida;
private String quantidade;
private String preco;

public String getComida() {
    return comida;
}

public void setComida(String comida) {
    this.comida = comida;
}

public String getQuantidade() {
    return quantidade;
}

public void setQuantidade(String quantidade) {
    this.quantidade = quantidade;
}

public String getPreco() {
    return preco;
}

public void setPreco(String preco) {
    this.preco = preco;
}

public String toString(){
    return comida;
}




}
    
asked by anonymous 21.06.2017 / 22:46

1 answer

0

Your RecyclerView expects an ArrayList and you are passing only a single item from the first Activity to the second.

If you want to show only this item, you have to modify the ArrayList initialization of the second Activity that is wrong. That way, by adding the only item in the ArrayList that you defined:

ArrayList<Cart> eList = new ArrayList<>();
eList.add(cart);
    
22.06.2017 / 03:33