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

Asked

Viewed 184 times

0

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

When the second activity is started it brings no information. Before trying to put in recyclerview tried using Textview and it worked, however when replacing to a Recycler does not bring any information. I believe it’s simple, but since I’m a beginner, I have no idea.

Below follows the code of the project.

Thanks for your 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);


    }
}
}

Cart.class Serializable

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;
}




}

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 startup of the second Activity that is wrong. Thus, adding the only item in Arraylist that you have defined:

ArrayList<Cart> eList = new ArrayList<>();
eList.add(cart);
  • Man, the procedure went right! It really helped me, vlw!

Browser other questions tagged

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