本文介绍了如何从strings.xml获取数据库sqlite数据,所以当我更改语言环境时,所有数据都可以转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中保存了所有数据,我该如何更改它并从字符串,xml中获取数据,所以当我更改语言环境时,所有数据都可以转换为语言环境,这是我的代码:

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);
    }


}

我尝试了R.string.data,但是我得到的只是数字,我没有得到保存在xml中的名称,请帮助我.预先谢谢您...:)

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... :)

推荐答案

此示例适用于英语/德语

This example of do it for English/German

字符串资源英文

<?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>

德语

<?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 _ 放在想要的阵列之前,以减少阵列数.
  • 建议使用
  • 文件名创建语言环境目录和资源文件
    • db_ before wanted ones to make less array.
    • filenames advised as Create locale directories and resource files
    • 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;
          }
      }
      

      • 做得很快,所以可能会好起来
      • 如果需要,
      • 将执行许多前缀(不仅仅是 db _ ),但可能会错误地测试多个前缀.
        • done fastly so might be bettered
        • will do many prefixes (more than just db_) if needed but might wrong no tested more than 1.
        • 您的DB_Sqlite类仅更改和添加了方法

          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;
          }
          

          活动测试

          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();
              }
          }
          

          此操作记录到日志英语:-

          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: <<<<<
          

          这篇关于如何从strings.xml获取数据库sqlite数据,所以当我更改语言环境时,所有数据都可以转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:34