Firebase Realtime Database : Recycler View Not Populating
Solution 1:
You are getting the following error:
com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: price
Because you have in your items
class a field called Price
which is defined as public
. This means that you'll be able to set a value directly on the fields, without the need of a setter, which is provided by default. Besides that, a getter will also be provided for you by default. Since you explicitly added a new getter in your class:
publicStringgetPrice() {
returnPrice;
}
It means that you have duplicate getters. This means that the one from your code is conflicting with the default one, hence the above error.
To solve this, you should simply set all the properties in your items
class as private
. In this way, you'll tell the compiler that only one getter is available.
Even if you do the following change:
privateString Price;
There is still an issue. So the problem in your code lies in the fact that you have in your items
class a field named Price
and the getter you have defined is called getPrice()
, which is not correct since Firebase is looking in the database for a field named price
and not Price
. See the lowercase p
letter vs. capital letter P
?
Now, there are two ways in which you can solve this problem. The first one would be to change your model class by renaming the fields according to the Java Naming Conventions. So your model class should look like this:
classItems {
privateString price, stock, product;
publicItems() {}
publicStringgetPrice() {
return price;
}
publicvoidsetPrice(String price) {
this.price = price;
}
publicStringgetStock() {
return stock;
}
publicvoidsetStock(String stock) {
this.stock = stock;
}
publicStringgetProduct() {
return product;
}
publicvoidsetProduct(String product) {
this.product = product;
}
}
See in this example, there are private
fields (lower-case) and public
getters. Besides that, the class name starts with I
capital.
There is also a simpler solution, which is to set the value directly on public fields with a minimum declaration of the class, like this:
classItems{
publicString price, stock, product;
}
Now just remove the current data and add it again using the correct (lower-case) names. This solution will work only if you are in the testing phase.
There is also another approach, which is to use "annotations". So if you prefer to use private fields and public getters, you should add the PropertyName annotation only in front of the getters. So your Items
class should look like this:
classItems {
privateString price, stock, product;
publicItems() {}
@PropertyName("Price")
publicStringgetPrice() {
return price;
}
@PropertyName("Price")
publicvoidsetPrice(String price) {
this.price = price;
}
@PropertyName("Stock")
publicStringgetStock() {
return stock;
}
@PropertyName("Stock")
publicvoidsetStock(String stock) {
this.stock = stock;
}
@PropertyName("Product")
publicStringgetProduct() {
return product;
}
@PropertyName("Product")
publicvoidsetProduct(String product) {
this.product = product;
}
}
Meaning that you tell Firebase to look after fields that start with a capital letter.
Post a Comment for "Firebase Realtime Database : Recycler View Not Populating"