简体   繁体   中英

Android Room Database

I can't wrap my head around how should i get my data without much boilerplate.

The problem: I have a database that i cannot alter. Which has multiple field of same type almost same for example i have text_en and text_fr (both are the same word in different language English and French) and i got + 71 same field but different languages.

What I need is something like

@Entitiy(tableName = "blabla")
class X {

    private String textTarget;
    private String textMain;

    ...
}

How should I do my Dao interface to get desired language and map into x class

what should work is to update entity ColumnInfo(name ="text_en") for example.

@Query("select  :main , :target from phrases where :id ")
    List<X> getPhrase(String main,String target);


usage : getPhrase("text_en","text_esp");
   // for example returning object X with field main = "hello" and target " holla")

The above example return the following error:

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

What you put in @Query is an SQL-statement, you can actually test them in sqlite command line utility or any desktop software to verify their correctness. So if you want a translation to the desired language, it should look like this:

@Query("SELECT :main AS text_main, :target AS text_target FROM `phrases` WHERE id = :id)
List<Translation> getTranslationById(String firstLang, String secondLang, long id);

Where Translation should be something like this:

class Translation {
    @ColumnInfo("text_main")
    String main;
    @ColumnInfo("text_target")
    String target;

    //setters, getters, etc
}

This class is used only as a return value from the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM