Issue
I Have this tow POJOs For RoomDB Entity
@Entity
public class Favorite {...}
@Entity
public class ProductCart {
Integer productQuantity;
...
}
I need to apply this select
statment and return it inside the object FavoriteAdapterData
SELECT favorite.*,
(SELECT CASE count(productQuantity)
WHEN 0 THEN 0
ELSE productQuantity
END FROM ProductCart
WHERE favorite.id = productId) AS cartQuantity
FROM Favorite favorite
public class FavoriteAdapterData {
int cartQuantity = 0;
Favorite favorite;
...
}
I try the following code
@Query("SELECT " +
" ( SELECT CASE count(productQuantity) " +
" WHEN 0 THEN 0 " +
" ELSE productQuantity " +
" END " +
" FROM ProductCart " +
" WHERE productId = favorite.id" +
" ) AS cartQuantity" +
" ,favorite.* " +
"FROM Favorite favorite")
LiveData<List<FavoriteAdapterData>> getFavoriteProducts();
But It didn't work and Android Studio notfi me this error:
Cannot figure out how to read this field from a cursor.
Favorite favorite;
^
Solution
You need to Embed the Favorite class within the FavoriteAdapterData class using the @Embedded annotation, then it will determine the columns and outputfields from the Favorite class's fields/variables
e.g. :-
public class FavoriteAdapterData {
int cartQuantity = 0;
@Embedded
Favorite favorite;
...
}
Answered By - MikeT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.