我正在尝试列出来自可序列化对象的数据,但它不起作用。
我列出了带有序列化对象的recyclerview的信息。传递给另一个活动时,recyclerview不会显示任何数据。
对不起我的英语。
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);
}
}
}
适配器
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;
}
}
答案 0 :(得分:0)
您遗失了adapter.notifyDataSetChanged()
并且您正在传递空的arraylist eList
ArrayList<Cart> eList = new ArrayList<>();
eList.add(cart);
Adapter adapter =new Adapter(getApplicationContext(), eList );
rvCart.setAdapter(adapter);
传递序列化对象
解决方案1(Hackytack解决方案)
公共类CardListHolder实现Serializable { private ListcardList = new ArrayList&lt;&gt;();
public List<Card>getCardList(){
return cardList;
}
public void setCardList(List<Card> cardList){
this.cardList=cardList;
}
}
使用此持有者对象传递卡片列表
解决方案2
为Card对象实现Parcelable,它可以通过卡列表,但理解起来有点复杂。请尝试www.parcelabler.com自动生成Card的Parceable类。