2

I am trying to retrieve a HashMap from Firebase to populate my Firebase RecyclerView below is my model class that I am trying to retrieve:

public class Employee{
private HashMap<String, Person> PeopleMap; 
// =  new HashMap<String, Person>();

public Employee () {
}

public Employee(HashMap<String, Person> people) {
    this.PeopleMap= people;
}

public HashMap<String, Person> getMap() {
    return PeopleMap;
}
public void setMap(HashMap<String, Person> people) {
    this.PeopleMap= people;
}
}

Person class

public class Person {
private String ImageUrl;
private  String Name;
private String  Pay;
public Person() {
}

public Person(String name, String imageUrl, String pay, 
 ) {
    ImageUrl = imageUrl;
    Name = name;
    Pay = pay;

}

public String getImageUrl() {
    return ImageUrl;
}

public void setImageUrl(String imageUrl) {
    ImageUrl = imageUrl;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getPay() {
    return Pay;
}

public void setPrice(String pay) {
    Pay= pay;
}
}

Now the problem is that when i retrieve my Employe model class in firebase recycler it is not null but if i retrieve my Hashmap from there the size of map is zero while there are items on the map below is what i am doing

Firebase REycler

        @Override
        protected void onBindViewHolder( ConvoViewHolder holder,  int 
        position,  Employee model) {
            if (model.equals(null)){
                Toast.makeText(OrderConversation.this, "object is null", 
                Toast.LENGTH_SHORT).show();
            }
            else
            {
                HashMap<String, OrderItem> cartitems =  model.getCart();
                int i = cartitems.size();
             if (i==0)
                    {
                        Toast.makeText(OrderConversation.this, "null 
                        map", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(OrderConversation.this, "size of 
                        map in bind"+ Integer.toString(i),
                        Toast.LENGTH_SHORT).show();
                    }
            }
        }
    };
    mEmpList.setAdapter(adapter);
    adapter.startListening();

My Database structure is like below with employee as a parent node and

enter image description here

Sorry I had to type it i hope it is clear to everyone about what kind of str i have. i want to retrieve from department node so that i can get two object (DHead1 & Dhead2 ), which have different no of elements in them as Dhead has 3 objects of person class and Dhead2 has 2. so one way i can go is either i load them first into a list and them use them or the other way that i am using now i.e. retrieve them as an object which contains the map of person class object.

i tried this below and i am getting values fine but inside onbindview its not working

mDatabase.addValueEventListener(new ValueEventListener() {
             @Override
             public void onDataChange(DataSnapshot dataSnapshot) {
                 for(DataSnapshot ds: dataSnapshot.getChildren()){
                     GenericTypeIndicator<Map<String, Person>> to = new 
                     GenericTypeIndicator<Map<String, Person>>() {};
                     Map<String, Person> map = ds.getValue(to);
                     for(Person ml:convoModels.values()) {
                         String name = ml.getName();
                         Toast.makeText(context, "name is "+ name, 
                         Toast.LENGTH_SHORT).show();
                     }
                   }
              }
             @Override
             public void onCancelled(DatabaseError databaseError) {
             }
         });
Jimmy
  • 87
  • 2
  • 9
  • **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Aug 02 '18 at 09:08
  • @AlexMamo I want to retrieve a Employee model object from firebase. I am following the same steps but my model object contains a hashmap which i want to use that hasmap but my model class is returning null here why is that – Jimmy Aug 02 '18 at 15:49
  • i have stored my Person class object in firebase and i want to retrieve a list of those objects under a node from firebase. Is there any other way to get an object(with list of objects inside) from firebase – Jimmy Aug 02 '18 at 15:52
  • In this case, please add your database structure. – Alex Mamo Aug 02 '18 at 16:05
  • i have updated it please see if you can help and sorry i don't have the system with me right now otherwise i would have uploaded the exact structure but i have tried to simplify it – Jimmy Aug 02 '18 at 16:58
  • The strcuture is ok but I need to see how your fields are stored in your real database (a simple screenshot will be fine). – Alex Mamo Aug 03 '18 at 08:41
  • @AlexMamo i have uploaded a pic is this what you want to see ? – Jimmy Aug 03 '18 at 09:34

1 Answers1

2

You are not getting anything because the fields in your model class are different than the fileds in your database. See, in your database you have imageUrl and in your model class you have ImageUrl. Keep in mind that when you are using a getter named getImageUrl(), Firebase will search for a field named imageUrl and not ImageUrl. The key for solving your problem is to change the name of your fields so it can start with a lower case like this:

private String imageUrl;
private  String name;
private String  pay;

Edit: See also the correct way of using the Employee class.

public class Employee {
    private HashMap<String, Person> peopleMap;

    public Employee() {}

    public Employee(HashMap<String, Person> peopleMap) {
        this.peopleMap = peopleMap;
    }

    public HashMap<String, Person> getPeopleMap() {
        return peopleMap;
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • the app crashes as soon as the values are called, it shows error calling size() on a null object – Jimmy Aug 03 '18 at 11:14
  • This was also the bahaviour before? – Alex Mamo Aug 03 '18 at 11:15
  • yes, i was sure that something is wrong with my model class, but now i think when i call the values from the database the values are not coming as hashmap of object of my model class – Jimmy Aug 03 '18 at 11:19
  • The problem in your code is that `model.getCart()` return `null` and this also because you model class is wrong. Your model should look like in my updated answer. Does it work now? – Alex Mamo Aug 03 '18 at 11:24
  • Attempt to invoke virtual method 'boolean java.util.HashMap.equals(java.lang.Object)' on a null object reference still same error – Jimmy Aug 03 '18 at 11:45
  • what alex says about the variable names is super important since if they are different from the database and your model you cant reach any value to work with – Gastón Saillén Aug 03 '18 at 11:50
  • @GastónSaillén Thanks Gastón for your comment. You're right. – Alex Mamo Aug 03 '18 at 11:51
  • i have been throught this when it happened to me for about an entire day to figure it out back in the days haha – Gastón Saillén Aug 03 '18 at 11:51
  • @Jimmy Is the same `NullPointerException` error but it occures now in a different place. With other words, the fist (initial) issue is solved. The second error occurs on this line of code `model.equals(null)`? Or in another place? Place indicate the exact place. – Alex Mamo Aug 03 '18 at 11:54
  • no actually i changed the same code now it is still the same error on the same place – Jimmy Aug 03 '18 at 11:58
  • @GastónSaillén thank you for the info but now i have checked every variable but still cant get my hashmap from database – Jimmy Aug 03 '18 at 11:59
  • when i try to use the retrieved hashmap it displays - invoke virtual method 'boolean java.util.HashMap.equals(java.lang.Object)' on a null object reference – Jimmy Aug 03 '18 at 12:01
  • @Jimmy Please indicate us the exact line where you are getting this error. – Alex Mamo Aug 03 '18 at 12:24
  • @AlexMamo it is the same line where I am getting the error, i just changed a method thats all the hasmap is still null nothing has changed so far, the error is still there on the same line.i was trying to check something to i changed a method that all. – Jimmy Aug 03 '18 at 12:27
  • can you guys provide some code example / links where similar way of retrieving hashmap is used – Jimmy Aug 03 '18 at 12:47
  • @Jimmy When you are trying to get an object of your `Employee` class to the database, which has a single property, the `peopleMap` map, point the reference on the a particula node which must be of type `Employee` and then use the getter to actually get the map back, right? – Alex Mamo Aug 03 '18 at 12:50
  • Is there everything alright, can I help you with other informations? – Alex Mamo Aug 04 '18 at 08:16
  • @AlexMamo i am still stuck and i tried using listener on my database the model class is fine and it is showing values check above i have added some code i used GenericTypeIndicator to retrieve values and they are coming fine. – Jimmy Aug 06 '18 at 10:48
  • There is no need to use a `GenericTypeIndicator` just follow my last comment. – Alex Mamo Aug 06 '18 at 10:53
  • @AlexMamo i used GenericTypeIndicator just to check whether my model class is correct or not and turns out its working fine. and about your last comment i am calling the same node i dont know why it is still returning null. – Jimmy Aug 06 '18 at 14:07
  • @AlexMamo i tried using a custom adapter to populate my recycler view, but still am getting some issues withit can you also look at this https://stackoverflow.com/questions/51706961/hashmap-is-not-attached-to-adapter it is related to the same problem. i am loading these values in a map first and then using the same map to populate the view but somehow my map is gettng attached to the adapter. – Jimmy Aug 06 '18 at 14:15
  • @AlexMamo i use same node point in both the cases and the result is coming on only when i use GenericTypeIndicator – Jimmy Aug 07 '18 at 11:00
  • @AlexMamo When i use GenericTypeIndicator it is giving me value fine but not inside firebase adapter. – Jimmy Aug 07 '18 at 13:03
  • what is mean is its working inside valueeventlistener(see the code above) on the same database node using the GenericTypeIndicator. – Jimmy Aug 07 '18 at 13:07
  • I understand and do you think that my answer helped you? – Alex Mamo Aug 07 '18 at 13:07
  • yes a little bit i will give you one up, i trying some other way here also but i dont know why my Map is getting attached to my Adapter can you please help me with this i am stuck here i cant use firebase adapter and now i cant use custom adapter also please have a look at it https://stackoverflow.com/questions/51706961/hashmap-is-not-attached-to-adapter it will be a great help thanks – Jimmy Aug 07 '18 at 13:20
  • This is basically another question. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you. – Alex Mamo Aug 07 '18 at 13:26
  • Yes i have posted it as another question that's why I provided the link above it is the question that i posted yesterday, i tried the other way for using my values but somehow my Map is not getting attached to my adapter can you please have a look at it – Jimmy Aug 07 '18 at 19:13