在RecyclerView(CardView)

时间:2017-11-28 12:33:28

标签: java android json android-recyclerview

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&#34 ;:" 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制作了对象类。

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。我不知道下一步该做什么,我在这里被封锁了。

1 个答案:

答案 0 :(得分:0)

您可以为List Items创建一个带有setter getter的bean类,bean类应该实现parcelable,以便您可以将bean对象发送到其他活动。对于recycler View,您使用相同bean类型的适配器,并在从服务器加载数据后设置该适配器。