-2

I want to fetch the price of all items in the form of list using recyclerview through realtime database firebase..i had attached a screenshot of firebase database please at that picture. link of screenshot of firebase is https://i.stack.imgur.com/b57gT.png]

this is main class

public class Clientapp extends AppCompatActivity {
RecyclerView recyclerView;
private List<Cartuser> usersList;
Clientjava adapter;
private DatabaseReference mDatabaserefrence;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_clientapp);
    usersList= new ArrayList<>();
    recyclerView=findViewById(R.id.recyclerclient);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    mDatabaserefrence= FirebaseDatabase.getInstance().getReference().child("checkorder").child("order");
    mDatabaserefrence.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
                String p;

                Cartuser users=dataSnapshot1.getValue(Cartuser.class);
                usersList.add(users);


            }
            adapter=new Clientjava(usersList,Clientapp.this);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


}

}

this is adapter class

 public class Clientjava extend 
RecyclerView.Adapter<Clientjava.ViewHolder> {private List<Cartuser> usersList;
       private Context context;
     public Clientjava(List<Cartuser> usersList, Context context) {
    this.usersList = usersList;
    this.context = context;}
   @Override
  public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
  viewType) {
    View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.clientappdesign,parent,false); return  new Clientjava.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.price.setText(usersList.get(position).getPrice());
}
@Override
public int getItemCount() {
    return usersList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
    private TextView price;
    private View mview;
    public ViewHolder(View itemView) {
        super(itemView);
        mview = itemView;
        price=mview.findViewById(R.id.totalclient); }
}

}

Egarage e
  • 21
  • 6

1 Answers1

0

Please edit your firebase code :

mDatabaserefrence = FirebaseDatabase.getInstance().getReference().child("checkorder").child("order");
    mDatabaserefrence.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            try {
                for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {

                    JSONObject prodinfo = new JSONObject(childDataSnapshot.getValue().toString());
                    Iterator iterator = prodinfo.keys();
                    JSONObject item = null;
                    while (iterator.hasNext()) {
                        String key = (String) iterator.next();
                        item = prodinfo.getJSONObject(key);

                        Cartuser users = new Cartuser(item.getString("name"),
                                item.getString("price"),
                                item.getString("weight"),
                                item.getString("hindi"));
                        usersList.add(users);
                    }


                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            adapter = new Clientjava(usersList, Clientapp.this);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
Tanvir Dalal
  • 165
  • 7
  • please look the screenshot in the link https://i.stack.imgur.com/b57gT.png]...from here i want to fetch price which is in nested key – Egarage e Aug 02 '19 at 04:45
  • Please follow https://stackoverflow.com/questions/42257480/retrieving-nested-data-in-firebase-android – Tanvir Dalal Aug 02 '19 at 04:48
  • is there any easy way i am not able to understand that code ...which you have described in your link\ – Egarage e Aug 02 '19 at 04:55