Search code examples
androidsqliteandroid-roomandroid-architecture-componentsandroid-jetpack

RoomDB - Count By Type Return Type


Am trying to count the number of items for a particular group similar to the question @ below link.

Original Question

Am Using Android RoomDB and am looking for the return type. DO I need to create a custom object or can I use a Map?

Putting the Original Question here:

I have a table:

ref,type
1,red
2,red
3,green
4,blue
5,black
6,black

I want the result of a sqlite query to be:

red,2
green,1
blue,1
black,2

P.S: Not sure if I should have commented on it there. Posted it as a new question since it pertains to Room DB.

When I use a Map like LiveData< Map < String,Long>> , it gives me below error ::

Not sure how to convert a Cursor to this method's return type


Solution

  • Yes, you have to create a POJO class and then use it as return type.I have seen samples and used it in my projects maybe that's the right way to do it. You can do something like this:

    class MyColorPojo {
        String name;
        int count;
    }
    
    @Query("Your query here")
    LiveData<List<MyColorPojo>> getValues();