-1

I have a JSON node which is displayed in logcat and look like this:

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

But I want from this JSON response to display in another activity just 3 of them "numarProduse":"4", "pretTotal":"1138", "numarClient":0.

I tried something else, to upload data to Firebase and then retrieve data from Firebase.

Here is my 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);
    }
}
}

I made my layout for CardView, and object class with getters and setters.

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

And here is where I tried to retrieve data from 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);
        }

But is not working, I get two columns where is 0 displayed. I have no idea what to do next, here I'm blocked.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Clau
  • 55
  • 9
  • youe json response is true – Ratilal Chopda Nov 28 '17 at 12:37
  • Possible duplicate of [ANDROID, Parse JSON data from a web server and display on ListView](https://stackoverflow.com/questions/36221795/android-parse-json-data-from-a-web-server-and-display-on-listview) – ADM Nov 28 '17 at 12:39
  • Now you have to implement the hard stuff. I would recommend you create a class for the Json response with getters and setters so this way you can call out the information whenever needed. You will also have to create a custom listview (another XML design) which will be the design of each item in your list. Then an adapter class which will work in conjunction to your listview and set the values where necessary. It's well complicated but I would recommend you to follow tutorial on custom listview. – Nero Nov 28 '17 at 12:54
  • and how can i set value for them ? – Clau Nov 28 '17 at 13:08
  • Please take a look, I did some changes. – Clau Nov 28 '17 at 13:23

1 Answers1

0

You can create a bean class with setter getter for your List Items and the bean class should implement parcelable so that you can send the bean object to other activity. For recycler View you use adapter of same bean type and set that adapter once data is loaded from server.