how to get DB sqlite data from strings.xml so when i change the locale all the data can be translated

ぃ、小莉子 提交于 2020-01-15 02:32:22

问题


i have database with all data saved inside it how can i change that and make get the data from strings,xml so when i change the locale all the data can be translated to locale language here is my code:

public class DB_Sqlite extends SQLiteOpenHelper {
    public static final String BDname = "data.db";
    public static final int DBVERSION = 1; /*<<<<< ADDED BUT NOT NEEDED */
    public static final String TABLE_FAVOURITES = "mytable";

    public static final String FAVOURITES_COL_ID = BaseColumns._ID; /*<<<< use the Android stock ID name*/
    public static final String FAVOURITES_COL_NAME = "name";
    public static final String FAVOURITES_COL_FAVOURITEFLAG = "favourite_flag"; /*<<<<< NEW COLUMN */

    public DB_Sqlite(@Nullable Context context) {
        super(context, BDname, null, DBVERSION /*<<<<< used constant above */);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_FAVOURITES + " (" +
                FAVOURITES_COL_ID + " INTEGER PRIMARY KEY," + /*<<<<< AUTOINCREMENT NOT NEEDED AND IS INEFFICIENT */
                FAVOURITES_COL_NAME + " TEXT, " +
                FAVOURITES_COL_FAVOURITEFLAG + " INTEGER DEFAULT 0" + /*<<<<< COLUMN ADDED */
                ")");

        /* Note indicator will set to non_favourite i.e. 0 */
        ContentValues cv = new ContentValues();
        cv.put(FAVOURITES_COL_NAME,"Complex Complex");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Trix Complex");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Kingdoms");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Complex Complex Partner");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Trix Complex Partner");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Kingdoms Partner");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Balot");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Estimation");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Tarneeb 31");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Tarneeb 61");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Tarneeb 41 Syrian");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Tarneeb 41 Egyptian");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Tarneeb 400");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Kasra");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Kasra Partner");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Bent AlSbeet");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Leekha");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Hand 2 Players");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Hand 3 Players");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Hand 4 Players");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Hand 5 Players");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Hand Partner");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Basra 2 Players");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Basra 3 Players");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Basra 4 Players");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Nathalah");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Kout Bo 4");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Kout Bo 6");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Domino 2 Players");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Domino 3 Players");
        db.insert(TABLE_FAVOURITES,null,cv);
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,"Domino 4 Players");
        db.insert(TABLE_FAVOURITES,null,cv);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVOURITES);
        onCreate(db);

    }
    public boolean insertData(String name){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(FAVOURITES_COL_NAME, name);
        long result = db.insert(TABLE_FAVOURITES,null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }


    public Cursor getFavouriteRows(boolean favourites  {
        SQLiteDatabase db = this.getWritableDatabase();
        String whereclause = FAVOURITES_COL_FAVOURITEFLAG + "=?";
        String compare = "<1";
        if (favourites) {
            compare =">0";
        }

        return db.query(
                TABLE_FAVOURITES,null,
                FAVOURITES_COL_FAVOURITEFLAG + compare,
                null,null,null,null
        );
    }


    private int setFavourite(long id, boolean favourite_flag) {
        SQLiteDatabase db = this.getWritableDatabase();
        String whereclause = FAVOURITES_COL_ID + "=?";
        String[] whereargs = new String[]{String.valueOf(id)};
        ContentValues cv = new ContentValues();
        cv.put(FAVOURITES_COL_FAVOURITEFLAG,favourite_flag);
        return db.update(TABLE_FAVOURITES,cv,whereclause,whereargs);
    }


    public int setAsFavourite(long id) {
        return setFavourite(id,true);
    }


    public int setAsNotFavourite(long id) {
        return setFavourite(id, false);
    }


}

i tried R.string.data but all i get is numbers i dont get the name that is saved in the xml please help me. Thank You in Advance... :)


回答1:


This example of do it for English/German

String Resources English

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">TheApp</string>
    <string name="db_bread">Bread</string>
    <string name="db_butter">Butter</string>
    <string name="db_jam">Jam</string>
    <string name="db_toast">Toast</string>
    <string name="db_boiledegg">Boiled Egg</string>
    <string name="db_friedegg">Fried Egg</string>
    <string name="db_poachedegg">Poached Egg</string>
    <string name="db_omlette">Omlette</string>
    <string name="db_bacon">Bacon</string>
</resources>

German

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Der App</string>
    <string name="db_bread">Brot</string>
    <string name="db_butter">Butter</string>
    <string name="db_jam">Marmelade</string>
    <string name="db_toast">Toast</string>
    <string name="db_boiledegg">Gekochtes Ei</string>
    <string name="db_friedegg">Spiegelei</string>
    <string name="db_poachedegg">Pochiertes Ei</string>
    <string name="db_omlette">Omelette</string>
    <string name="db_bacon">Speck</string>
</resources>
  • db_ before wanted ones to make less array.
  • filenames advised as Create locale directories and resource files

Class for StringResourcesHandling

public class StringResourcesHandling {

    private static final String[] allowedStringResourcePrefixes = new String[]{"db_"};
    private static boolean loaded = false;
    private static Field[] fields = R.string.class.getFields();
    private static ArrayList<String> allowedStringResourceNames = new ArrayList<>();

    private static void loadStringResources() {
        if (loaded) return;

        for (Field f: fields) {
            if (isResourceNameAllowedPrefix(f.getName())) {
                allowedStringResourceNames.add(f.getName());
            }
        }
        loaded = true;
    }

    private static boolean isResourceNameAllowedPrefix(String resourceName) {
        if (allowedStringResourcePrefixes.length < 1) return true;
        for (String s: allowedStringResourcePrefixes) {
            if (resourceName.substring(0,s.length()).equals(s)) return true;
        }
        return false;
    }

    public static String getStringByName(Context context, String name) {
        String rv = "";
        boolean nameFound = false;
        if (!loaded) {
            loadStringResources();
        }
        for (String s: allowedStringResourceNames) {
            if (s.equals(name)) {
                nameFound = true;
                break;
            }
        }
        if (!nameFound) return rv;
        return context.getString(context.getResources().getIdentifier(name,"string",context.getPackageName()));
    }

    public static List<String> getAllStringResourceNames() {

        if (!loaded) {
            loadStringResources();
        }
        return allowedStringResourceNames;
    }
}
  • done fastly so might be bettered
  • will do many prefixes (more than just db_) if needed but might wrong no tested more than 1.

Your DB_Sqlite class changed and added methods only

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_FAVOURITES + " (" +
            FAVOURITES_COL_ID + " INTEGER PRIMARY KEY," + /*<<<<< AUTOINCREMENT NOT NEEDED AND IS INEFFICIENT */
            FAVOURITES_COL_NAME + " TEXT, " +
            FAVOURITES_COL_FAVOURITEFLAG + " INTEGER DEFAULT 0" + /*<<<<< COLUMN ADDED */
            ")");
    /* CHANGES HERE BELOW loop adding all Resource names NOT VALUES */
    ContentValues cv = new ContentValues();
    for (String s: StringResourcesHandling.getAllStringResourceNames()) {
        cv.clear();
        cv.put(FAVOURITES_COL_NAME,s); //<<<<<<<<<< Name of the resource
        db.insert(TABLE_FAVOURITES,null,cv);
    }
}

/* Getting everything and make MatrixCursor VALUES from Resource names from Cursor with Resource names  */
public Cursor getAllDataInCurrentLocale(Context context) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor csr = db.query(TABLE_FAVOURITES,null,null,null,null,null,null);
    if (csr.getCount() < 1) return csr;
    MatrixCursor mxcsr = new MatrixCursor(csr.getColumnNames(),csr.getCount());
    while (csr.moveToNext()) {
        mxcsr.addRow(convertCursorRow(context,csr,new String[]{FAVOURITES_COL_NAME}));
    }
    csr.close();
    return mxcsr;
}

/* This getting columns from Cursor into String array (no BLOB handleing)*/
private String[] convertCursorRow(Context context, Cursor csr, String[] columnsToConvert) {
    String[] rv = new String[csr.getColumnCount()];
    for (String s: csr.getColumnNames()) {
        boolean converted = false;
        for (String ctc: columnsToConvert) {
            if (csr.getType(csr.getColumnIndex(s)) == Cursor.FIELD_TYPE_BLOB) {
                //........ would have to handle BLOB here if needed (another question if needed)
            }
            if (ctc.equals(s)) {
                rv[csr.getColumnIndex(s)] = StringResourcesHandling.getStringByName(context,csr.getString(csr.getColumnIndex(s)));
                converted = true;
            }
        } if (!converted) {
            rv[csr.getColumnIndex(s)] = csr.getString(csr.getColumnIndex(s));
        }
    }
    return rv;
}

Activity testing

public class MainActivity extends AppCompatActivity {

    DB_Sqlite dbSqlite;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* Show the resources for demo */
        for(String s: StringResourcesHandling.getAllStringResourceNames()) {
            Log.d("RESOURCEDATA","String Resource Name = " + s +
                    "\n\tValue = " + StringResourcesHandling.getStringByName(this,s)
            );
        }

        dbSqlite = new DB_Sqlite(this);
        Cursor csr = dbSqlite.getAllDataInCurrentLocale(this);
        DatabaseUtils.dumpCursor(csr);
        csr.close();
    }
}

This do to the log English :-

2020-01-04 21:50:43.105 18344-18344/? D/RESOURCEDATA: String Resource Name = db_bacon
        Value = Bacon
2020-01-04 21:50:43.105 18344-18344/? D/RESOURCEDATA: String Resource Name = db_boiledegg
        Value = Boiled Egg
2020-01-04 21:50:43.106 18344-18344/? D/RESOURCEDATA: String Resource Name = db_bread
        Value = Bread
2020-01-04 21:50:43.106 18344-18344/? D/RESOURCEDATA: String Resource Name = db_butter
        Value = Butter
2020-01-04 21:50:43.106 18344-18344/? D/RESOURCEDATA: String Resource Name = db_friedegg
        Value = Fried Egg
2020-01-04 21:50:43.106 18344-18344/? D/RESOURCEDATA: String Resource Name = db_jam
        Value = Jam
2020-01-04 21:50:43.106 18344-18344/? D/RESOURCEDATA: String Resource Name = db_omlette
        Value = Omlette
2020-01-04 21:50:43.106 18344-18344/? D/RESOURCEDATA: String Resource Name = db_poachedegg
        Value = Poached Egg
2020-01-04 21:50:43.106 18344-18344/? D/RESOURCEDATA: String Resource Name = db_toast
        Value = Toast
2020-01-04 21:50:43.119 18344-18344/? I/System.out: >>>>> Dumping cursor android.database.MatrixCursor@283a643
2020-01-04 21:50:43.119 18344-18344/? I/System.out: 0 {
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    _id=1
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    name=Bacon
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    favourite_flag=0
2020-01-04 21:50:43.119 18344-18344/? I/System.out: }
2020-01-04 21:50:43.119 18344-18344/? I/System.out: 1 {
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    _id=2
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    name=Boiled Egg
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    favourite_flag=0
2020-01-04 21:50:43.119 18344-18344/? I/System.out: }
2020-01-04 21:50:43.119 18344-18344/? I/System.out: 2 {
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    _id=3
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    name=Bread
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    favourite_flag=0
2020-01-04 21:50:43.119 18344-18344/? I/System.out: }
2020-01-04 21:50:43.119 18344-18344/? I/System.out: 3 {
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    _id=4
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    name=Butter
2020-01-04 21:50:43.119 18344-18344/? I/System.out:    favourite_flag=0
2020-01-04 21:50:43.119 18344-18344/? I/System.out: }
2020-01-04 21:50:43.119 18344-18344/? I/System.out: 4 {
2020-01-04 21:50:43.120 18344-18344/? I/System.out:    _id=5
2020-01-04 21:50:43.120 18344-18344/? I/System.out:    name=Fried Egg
2020-01-04 21:50:43.120 18344-18344/? I/System.out:    favourite_flag=0
2020-01-04 21:50:43.120 18344-18344/? I/System.out: }
2020-01-04 21:50:43.120 18344-18344/? I/System.out: 5 {
2020-01-04 21:50:43.120 18344-18344/? I/System.out:    _id=6
2020-01-04 21:50:43.120 18344-18344/? I/System.out:    name=Jam
2020-01-04 21:50:43.120 18344-18344/? I/System.out:    favourite_flag=0
2020-01-04 21:50:43.120 18344-18344/? I/System.out: }
2020-01-04 21:50:43.120 18344-18344/? I/System.out: 6 {
2020-01-04 21:50:43.120 18344-18344/? I/System.out:    _id=7
2020-01-04 21:50:43.120 18344-18344/? I/System.out:    name=Omlette
2020-01-04 21:50:43.121 18344-18344/? I/System.out:    favourite_flag=0
2020-01-04 21:50:43.121 18344-18344/? I/System.out: }
2020-01-04 21:50:43.121 18344-18344/? I/System.out: 7 {
2020-01-04 21:50:43.121 18344-18344/? I/System.out:    _id=8
2020-01-04 21:50:43.121 18344-18344/? I/System.out:    name=Poached Egg
2020-01-04 21:50:43.121 18344-18344/? I/System.out:    favourite_flag=0
2020-01-04 21:50:43.121 18344-18344/? I/System.out: }
2020-01-04 21:50:43.121 18344-18344/? I/System.out: 8 {
2020-01-04 21:50:43.121 18344-18344/? I/System.out:    _id=9
2020-01-04 21:50:43.122 18344-18344/? I/System.out:    name=Toast
2020-01-04 21:50:43.122 18344-18344/? I/System.out:    favourite_flag=0
2020-01-04 21:50:43.122 18344-18344/? I/System.out: }
2020-01-04 21:50:43.122 18344-18344/? I/System.out: <<<<<

Then do to the log German (RESOURCEDATA not see as no changed)

2020-01-04 21:53:06.220 I/System.out: >>>>> Dumping cursor android.database.MatrixCursor@283a643
2020-01-04 21:53:06.220 I/System.out: 0 {
2020-01-04 21:53:06.220 I/System.out:    _id=1
2020-01-04 21:53:06.220 I/System.out:    name=Speck
2020-01-04 21:53:06.220 I/System.out:    favourite_flag=0
2020-01-04 21:53:06.220 I/System.out: }
2020-01-04 21:53:06.220 I/System.out: 1 {
2020-01-04 21:53:06.220 I/System.out:    _id=2
2020-01-04 21:53:06.220 I/System.out:    name=Gekochtes Ei
2020-01-04 21:53:06.221 I/System.out:    favourite_flag=0
2020-01-04 21:53:06.221 I/System.out: }
2020-01-04 21:53:06.221 I/System.out: 2 {
2020-01-04 21:53:06.221 I/System.out:    _id=3
2020-01-04 21:53:06.221 I/System.out:    name=Brot
2020-01-04 21:53:06.221 I/System.out:    favourite_flag=0
2020-01-04 21:53:06.221 I/System.out: }
2020-01-04 21:53:06.221 I/System.out: 3 {
2020-01-04 21:53:06.221 I/System.out:    _id=4
2020-01-04 21:53:06.221 I/System.out:    name=Butter
2020-01-04 21:53:06.221 I/System.out:    favourite_flag=0
2020-01-04 21:53:06.221 I/System.out: }
2020-01-04 21:53:06.221 I/System.out: 4 {
2020-01-04 21:53:06.221 I/System.out:    _id=5
2020-01-04 21:53:06.221 I/System.out:    name=Spiegelei
2020-01-04 21:53:06.221 I/System.out:    favourite_flag=0
2020-01-04 21:53:06.221 I/System.out: }
2020-01-04 21:53:06.221 I/System.out: 5 {
2020-01-04 21:53:06.221 I/System.out:    _id=6
2020-01-04 21:53:06.221 I/System.out:    name=Marmelade
2020-01-04 21:53:06.221 I/System.out:    favourite_flag=0
2020-01-04 21:53:06.221 I/System.out: }
2020-01-04 21:53:06.221 I/System.out: 6 {
2020-01-04 21:53:06.221 I/System.out:    _id=7
2020-01-04 21:53:06.221 I/System.out:    name=Omelette
2020-01-04 21:53:06.221 I/System.out:    favourite_flag=0
2020-01-04 21:53:06.221 I/System.out: }
2020-01-04 21:53:06.221 I/System.out: 7 {
2020-01-04 21:53:06.221 I/System.out:    _id=8
2020-01-04 21:53:06.221 I/System.out:    name=Pochiertes Ei
2020-01-04 21:53:06.222 I/System.out:    favourite_flag=0
2020-01-04 21:53:06.222 I/System.out: }
2020-01-04 21:53:06.222 I/System.out: 8 {
2020-01-04 21:53:06.222 I/System.out:    _id=9
2020-01-04 21:53:06.222 I/System.out:    name=Toast
2020-01-04 21:53:06.222 I/System.out:    favourite_flag=0
2020-01-04 21:53:06.222 I/System.out: }
2020-01-04 21:53:06.222 I/System.out: <<<<<


来源:https://stackoverflow.com/questions/59587109/how-to-get-db-sqlite-data-from-strings-xml-so-when-i-change-the-locale-all-the-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!