0

Trying to retrieve a list of string from Firebase Database but i am keep getting this error "DatabaseException: Class java.util.List has generic type parameters, please use GenericTypeIndicator instead". I also have gone through other answers but none of them helped.

My code:


private void incrementCount(){
        reference = FirebaseDatabase.getInstance().getReference("devices").child("deviceIdList");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                List<String> list = new ArrayList<>();
                for(DataSnapshot ds : snapshot.getChildren()) {

                    list = ds.getValue(List.class);
                    Log.d("TAG", "size of deviceId Array" + list.size());
                }

                if (!list.contains(androidId)){
                    FirebaseDatabase database = FirebaseDatabase.getInstance();
                    DatabaseReference myRef = database.getReference("posts").child(postId);

                    HashMap<String, Object> hashMap = new HashMap<>();

                    hashMap.put("counter", counter++);
                    myRef.updateChildren(hashMap);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });


    }

json structure of my db. It only have one item for now:

"devices" : {
    "deviceIdList" : [ "b5ac388d1c1bf0c3", "j4l322n4ş2nş3n3ğ" ]
  },

2
  • @FrankvanPuffelen as i mentioned in my question i have already looked up all the answers and none of them worked for me. so this is not a duplicate. Commented Jun 16, 2021 at 15:47
  • You'll need to use a generic type indicator, as shown in here: stackoverflow.com/a/37688361 (and in Abhinav's answer below). If you've also tried that, edit your question to show the code for that please. Commented Jun 16, 2021 at 16:29

1 Answer 1

1

You cannot convert any object to generic List object. You need to define which type of objects the list accepts.

Firebase provides a GenericTypeIndicator to resolve this.

Usage:

GenericTypeIndicator<List<String>> typeIndicator = new GenericTypeIndicator<List<String>>() {};

And then pass typeIndicator as the type argument in your getValue() E g.

list = ds.getValue(typeIndicator)

Should solve your problem.

Thanks

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the ansvver. it now gives this = DatabaseException: Expected a List while deserializing, but got a class java.lang.String.
Sorry I missed the loop. You ideally don't need to iterate over children of snapshot. You can convert it directly to list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.