Home > OS >  firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.
firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.

Time:08-01

My Database:

enter image description here

My Logcat:

2022-07-31 16:04:14.850 6729-6729/com.droidbane.recipe E/ghj: com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.droidbane.recipe.database.models.Categories
2022-07-31 16:04:14.851 6729-6729/com.droidbane.recipe E/ghj: com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.droidbane.recipe.database.models.Categories
2022-07-31 16:04:14.852 6729-6729/com.droidbane.recipe E/ghj: com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type com.droidbane.recipe.database.models.Categories
2022-07-31 16:04:14.852 6729-6729/com.droidbane.recipe E/ghj: com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.droidbane.recipe.database.models.Categories

My Code :

public void getAllCategories() {
        myRef = database.getReference("categories").child(categoryID);

        myRef.addValueEventListener(new ValueEventListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                categoriesArrayList.clear();

                for (DataSnapshot data : snapshot.getChildren()) {
                    try {
                        Categories categories = data.getValue(Categories.class);

                        assert categories != null;

                        categories.setKey(data.getKey());

                        categoriesArrayList.add(categories);
                    }catch (Throwable throwable) {
                        Log.e("ghj", String.valueOf(throwable) );
                    }

                }

                System.out.println(categoriesArrayList);

                recipeAdapter.notifyDataSetChanged();
            }

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

            }
        });
}

I wanted to do:

I want to give the information of the category whose id I have specified for the RecyclerView.

I just want to get the information of the data selected with id

CodePudding user response:

The reference you build here:

myRef = database.getReference("categories").child(categoryID);

This points to a single category in your database, so when you then get the data for that category and loop over its child nodes in onDataChange, your data become each of the individual properties of that category (image, imageTwo, key, etc). So when you then call data.getValue(Categories.class), you're trying to convert the value of an individual property to a Categories object, which doesn't work.

The solution is to remove the loop from your onDataChange, and instead get the single category in the snapshot with:

Categories categories = snapshot.getValue(Categories.class)
  • Related