0

I am trying to pull inventory data and in the end display it on a listview with using an array adapter. Basically I need to add each item object to list then with itemobject.getName() I need to access itemname.

Users 
        UserID
                inventory
                      0
                        name:coffee
                        count:1
                      1
                        name:testitem
                        count:2

Item Class:

public class Item {

    private String name;
    private int count;
    public Item(){

    }
    public String getName(){
        return name;
    }
    public int getCount(){
        return count;
    }
}

This is how I take data:

inventoryview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mydb.child("users").child(uid).child("inventory").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            String data = dataSnapshot.getValue().toString();
                            Log.d("asd",data);
                            Item item = dataSnapshot.getValue(Item.class);
//iterate through incoming data and add item objects to list
         Log.d("itemname",item.getName());

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

                    Toast.makeText(getActivity(),
                            "Inventory clicked.",
                            Toast.LENGTH_LONG).show();
                }
            });
lastpeony4
  • 549
  • 1
  • 9
  • 22
  • You are also not saving data of the POJO to the database. You need to have a node called "Item" and inside of it nodes called "name" and "count" then you will be able to retrieve the data – Peter Haddad Apr 25 '18 at 12:51
  • So for multiple items how do i do that – lastpeony4 Apr 25 '18 at 12:55

4 Answers4

1

You're missing a loop in your onDataChange:

mydb.child("users").child(uid).child("inventory").addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
      Log.d("itemname", childSnapshot.getChild("name").getValue());
      Item item = childSnapshot.getValue(Item.class);
      Log.d("itemname",item.getName());
    }
  }
  @Override
  public void onCancelled(DatabaseError databaseError) {
    throw databaseError.toException(); // don't ignore errors
  }
}

If that first log statement prints the correct value and the second one doesn't, you have a mismatch between your Java class and the JSON structure.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

This is the correct way in which your model class should look like:

public class Item {
    private String name;
    private int count;

    public Item() {}

    public Item(String name, int count) {
        this.name = name;
        this.count = count;
    }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getCount() { return count; }
    public void setCount(int count) { this.count = count; }
}

It contains the public no-argument constructor and the corresponsing public setters and getters.

To create an item of Item class, just create a new object and pass it to the setValue() method like this:

Item item = new Item("Name", 1);
yourRef.setValue(item);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • i am trying to pull the inventory data and display each items name.How do i do that – lastpeony4 Apr 25 '18 at 12:51
  • In a very simple way. If you want retrieve data from the Firebase database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`, **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can do it. – Alex Mamo Apr 25 '18 at 12:55
  • I just want to access the items name that i pull from database.There can be multiple items in inventory i dont know how to structure it in database and how to structure it as a class.For example if structure is like this how i reach each items name and count ??? http://prntscr.com/j9y0le – lastpeony4 Apr 25 '18 at 13:03
  • So to accees only the item names, please follow the explanations in that post. The structure should be in the exact same way I showed you in my answer. – Alex Mamo Apr 25 '18 at 13:10
  • @AlexMamo Setters are not not required. If there is no setter for a JSON property, the Firebase client will set the value directly onto the field. A constructor-with-arguments is also not required. While both are idiomatic, there are good cases to have classes without them. – Frank van Puffelen Apr 25 '18 at 13:35
  • 1
    @FrankvanPuffelen You're right, I know we can omit them. I just tried to show the OP how a complete model class can look like according to what he asked, `How do i write correct Class for Item name`. There are situation in which the setters are much easier to use, in my opinion. By the time I came up with my answer, the OP had a complete other database structure, because in the meanwhile he edited the post. Now seeing your answer, I can say it will solve OP's issue for sure. – Alex Mamo Apr 25 '18 at 14:16
  • 2
    Ah that explains. No worries then. I just want to make sure we're all explicit about what things are required, vs which are idiomatic, vs which are personal preference. All three are fine, as long as it's clear. – Frank van Puffelen Apr 25 '18 at 14:34
0

change this

and also i hope you insert data according to item POJO class if different then not getting data.

mydb.child("Users").child("UserID").child("inventory")
0

Suppose your structure is as follows:

Root
  |___Item1
  |      |___name : "name1"
  |      |___count: "count1"
  |
  |___Item2
  |      |___name : "name2"
  |      |___count: "count2"
  |___Item3
  |      |___name : "name3"
  |      |___count: "count3"

I'd add a listener to Root and call dataSnapshot.getChildren() and iterate over the children using a for-each loop. Now, for each child, I can call data.getValue(Item.class) and be done with it.

Mayank Verma
  • 633
  • 6
  • 19