Can’t convert object of type java.util.ArrayList to type com.example.soas.POJO.Service

You’re missing a for loop over the 14 children in your JSON. In addition: you’re loading all Services, while you only need the ones for the current user, so your code is wasting bandwidth and likely less secure.

To fix both problems, you can listen one level lower in the JSON tree, and update the code on onDataChange to match:

reference.child(user).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        if (snapshot.exists()) {
            serviceList = new ArrayList<>();

            for (DataSnapshot ds: snapshot.getChildren()) {
                serviceList.add(ds.getValue(Service.class));

                ServiceAdapter serviceAdapter = new ServiceAdapter(serviceList);
                recyclerView = findViewById(R.id.serviceRecycler);
                recyclerView.setAdapter(serviceAdapter);
            }
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        Toast.makeText(Services.this, error.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top