Search code examples
android-roompojoandroid-viewpager2

Cannot figure out how to structure data for ViewPager2 from one Room entity using pojo


I am new to Android and need some help. I'm making an App which manages expenses. I have room database table 'expenses':

CostEntry.java

@Entity(tableName = "expenses")
public class CostEntry {

    @PrimaryKey(autoGenerate = true)
    private int id;
    private String category;
    private String name;
    private int cost;
    private String date;
    private String currency;

//....
}

My activity shows all the expenses on a certain date. Now, I want to make viewpager2 whose page will show all categories with theirs sums - in particular currency (number of pages equals number of different currencies).

So, I made a simple pojo:

TotalCostPojo.java

public class TotalCostPojo {

    public TotalCostPojo() {
    }

    private String currency;
    private List<String> category;
    private List<Integer> categoryCosts;


    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public List<String> getCategory() {
        return category;
    }

    public void setCategory(List<String> category) {
        this.category = category;
    }

    public List<Integer> getCategoryCosts() {
        return categoryCosts;
    }

    public void setCategoryCosts(List<Integer> categoryCosts) {
        this.categoryCosts = categoryCosts;
    }
}

But, I got an error:

error: Cannot figure out how to read this field from a cursor. private List categoryCosts; private List category;

This is my Dao:

@Query("SELECT DISTINCT currency, category, SUM (cost) AS categoryCosts FROM expenses WHERE date = :date group by category")
    LiveData<List<TotalCostPojo>> loadTotalCategoryCosts(String date);

I had similar problem before, but then I resolved it with @Relation annotations. I don't know how to do it here because there isn't more than one entity.

Thanks in advance.


Solution

  • I think in Room there is no built-in way for what you want (as you've written it's not a case for @Relation annotation, since there is no two real tables to join). I can only suggest workaround you can try (may be it's a obvious boilerplate, and you've expected for more elegance, but still is a way):

    1. Add class CostPojo with fields: currency(String), category(String), categoryCosts(Integer).
    2. Change the type of returning value in your loadTotalCategoryCosts to List<CostPojo>:
    LiveData<List<CostPojo>> loadTotalCategoryCosts(String date);
    
    1. To get the needed data structure for your ViewPager you have to make your own transformation function from List<CostPojo> to List<TotalCostPojo>. For this you can play with LiveData Transformations or for example to add static auxilary method convertToTotalCost to your CostPojo class (input - List<CostPojo>, output - List<TotalCostPojo>).

    UPDATE For those who interested - final solution based on this answer made by author of the question is inside separate answer