7

I know this error quite popular, I read lot about it here, but I'm still not sure what is the best for me.

I have service class (POJO) with this structure:

public class Service {
    private long priceShort;
    private String name;
    private String desc;
    List<String> employees = new ArrayList<>();  //recently added

    public Service() {  //default constructor

    }

    public Service(String name, String desc, long priceShort,ArrayList<String> employees ) {
        this.name = name;
        this.desc = desc;
        this.priceShort = priceShort;
        this.employees = employees;  //recently added
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public long getPriceShort() {
        return priceShort;
    }

    public void setPriceShort(long priceShort) {
        this.priceShort = priceShort;
    }


    public List<String> getEmployees() {
        return employees;
    }
    public void setEmployees(List<String> employess) {
        this.employees = employees;
    }
}

DB structure :

"sid1" : {  
  "name" : "Read",
  "desc" : "",
  "priceShort" : 1000,     
   "employees" : {  //recently added
    "John" : "true",
    "David" : "true",
    "Mike" : "true"
  }
},
"sid2" : {
  "name" : "Write",
  "desc" : "xxx",    
  "priceShort" : 2000,
   "employees" : {
    "David" : "true"
  }
}

An error appear in valueListener; here is my code:

private void listenServiceAll() {

    List<Service> serviceList = new ArrayList<Service>();
    mDatabase.child("ServicesPrice").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            serviceList.clear();
            for (DataSnapshot dsp : dataSnapshot.getChildren()) {
                serviceList.add(dsp.getValue(Service.class)); //add result into array list
            }

            reservationServiceAdapter.notifyDataSetChanged();

            // ...
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // ...
        }
    });
}

How can I solve this?

Note: I don't have an error before, everything is running well until I decided to change my structure and adding employee list in my service class List<String> employees = new ArrayList<>;.

But the reason I added that, was to be able to hold the value according to my structure (nested employee, I want to specify that each service can be done by particular employee) .

Update:

After checking the link from Sir Frank, I change my List<String> employees = new ArrayList<>(); to Map<String, Object> employees = new HashMap<String, Object>(); with setter and getter :

public void setValue(Map<String, Object> map)
{
    this.employees = map;
}
public Map<String, Object> getValue()
{
    return this.employees;
}

but after running the debug again, error disappeared but i can't get the employee value set into my object. (although i can see the datasnapshot actually has the value).

Update 2:

I'm actually able to get the value by iterate through the children:

 if (dsp.child("employees").getValue() != null) {
     for (DataSnapshot emp : dsp.child("employees").getChildren()) {
         listOfEmployee.add(emp.getKey());
     }
 }

I can proceed with this, but I wonder why I can get the value directly altogether by serviceList.add(dsp.getValue(Service.class)); even after adding the hashmap to my service class.

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
Goofy_Phie
  • 671
  • 1
  • 8
  • 25
  • What you have in your JSON is not a `List`. It is instead a `Map`. See my answer here: http://stackoverflow.com/questions/32886546/how-to-get-all-child-list-from-firebase-android/32888869#32888869 – Frank van Puffelen Apr 23 '17 at 17:28
  • @FrankvanPuffelen , thank you sir, please check my update above. – Goofy_Phie Apr 23 '17 at 18:09

0 Answers0