在RecyclerView中显示JSON数据(CardView)

q3qa4bjr  于 2023-05-02  发布在  其他
关注(0)|答案(1)|浏览(98)

我有一个JSON node,它显示在logcat中,看起来像这样:

jsonData[{"numarProduse":"4","pretTotal":"1138","data":"11-27-2017","numeVanzator":"Clau","numarClient":0,"detaliiCos":{"metodaPlata":"CASH"}}]

但我想从这个JSON response在另一个活动中只显示其中的3个**“numarProduse”:“4”,“pretTotal”:“1138”,“numarClient”:0**。
我尝试了其他方法,将数据上传到Firebase,然后从Firebase检索数据。

这里是我的RecyclerView Adapter

public class CardArrayAdapter  extends RecyclerView.Adapter<CardArrayAdapter.ViewHolder> {
private List<Card> listdata;

public CardArrayAdapter(List<Card> listdata) {
    this.listdata = listdata;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item_card, parent, false);
    return new ViewHolder(v);
}

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

    Card vanzatorProduse = listdata.get(position);

    holder.txtViewNumarCumparator.setText(vanzatorProduse.getProdusId());
    holder.listaProduse.setText(vanzatorProduse.getQty());
    holder.sumaProduse.setText(vanzatorProduse.getSubTotal());
}
@Override
public int getItemCount() {
    return listdata.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView txtViewNumarCumparator;
    public TextView listaProduse;
    public TextView sumaProduse;

    public ViewHolder(View itemView) {
        super(itemView);

        txtViewNumarCumparator = (TextView) itemView.findViewById(R.id.txtViewNumarCumparator);
        listaProduse = (TextView) itemView.findViewById(R.id.listaProduse);
        sumaProduse = (TextView) itemView.findViewById(R.id.sumaProduse);
    }
}
}

我为CardView和带有getter和setter的object类做了布局。

public class Card implements Serializable {
public String produsId;
public String subTotal;
public String qty;

public Card(){}

public Card(String produsId, String subTotal, String qty) {
    this.produsId = produsId;
    this.subTotal = subTotal;
    this.qty = qty;
}

public String getProdusId() {
    return produsId;
}

public void setProdusId(String produsId) {
    this.produsId = produsId;
}

public String getSubTotal() {
    return subTotal;
}

public void setSubTotal(String subTotal) {
    this.subTotal = subTotal;
}

public String getQty() {
    return qty;
}

public void setQty(String qty) {
    this.qty = qty;
}
}

下面是我试图从Firebase中检索数据的地方:

myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            list = new ArrayList<>();

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){

                VanzatorProduse produse = snapshot.getValue(VanzatorProduse.class);
                Card listdata = new Card();
                String id = produse.getProdusId();
                String qty = String.valueOf(produse.getQty());
                String suma = String.valueOf(produse.getSubTotal());

                listdata.setProdusId(id);
                listdata.setQty(qty);
                listdata.setSubTotal(suma);
                list.add(listdata);

            }
            CardArrayAdapter recycler = new CardArrayAdapter(list);
            RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(VanzatorActivity.this);
            recyclerview.setLayoutManager(layoutmanager);
            recyclerview.setItemAnimator( new DefaultItemAnimator());
            recyclerview.setAdapter(recycler);
        }

但不起作用,我得到两列,其中显示0。我不知道下一步该怎么办,我在这儿被卡住了。

t30tvxxf

t30tvxxf1#

你可以为你的List Items创建一个带有setter getter的bean类,这个bean类应该实现parcelable,这样你就可以把bean对象发送给其他Activity。对于回收器视图,您使用相同bean类型的适配器,并在从服务器加载数据时设置该适配器。

相关问题