0

When the user opens the app for the first time I'd like to insert into database some values.

Question 1: How is the best way to insert into database for the first time?

By now I'm using GreenDAO. Should I still continue using it or change it?

I also want to insert the values according to device's language. If the user uses english language so english values must be inserted. If user uses portuguese language so portuguese values must be inserted. And there we go. But if user change language on android settings I also want to update this values.

Question 2: How to update the database after change the device's language?

These values may be deleted by the user, so I want to update only the values that still are inserted.

My initial Class to be inserted:

public class CategoryList {

    private final String mLanguage;

    public CategoryList(String language) {
        mLanguage = language;
    }

    public List<Category> get() {
        final List<Category> list = new ArrayList<>();

        switch (mLanguage) {
            case "pt":
                list.add(new Category(1L, "Passeio", -13388315));
                list.add(new Category(2L, "Comida", -48060));
                break;
            default: // en
                list.add(new Category(1L, "Walk", -13388315));
                list.add(new Category(2L, "Food", -48060));
                break;
        }

        return list;
    }
}

I don't see how I'm going to update only the values that still are inserted.

Question 3: How to update only the values that still are inserted?

Any idea? Thanks.

Edit: I'm going to have hundreds of values (in my case Category type). As you can see in code above the Category has more columns than just 'name' only.

I'd like to insert the correct category according to device's language at first start.

I'd like to update these categories if the user changes the device's language on system.

I don't see how to use res, because of this I tried using this above class.

How to do this?

Michiyo
  • 1,161
  • 1
  • 14
  • 33
Douglas Fornaro
  • 2,017
  • 2
  • 22
  • 30
  • why you don't insert with default language(english or anything you want ) then change language with system locale when you want to show ? – Shayan Pourvatan May 08 '16 at 18:32

1 Answers1

0
  1. I've never used GreenDAO, so I can't comment on this.

  2. How about inserting the name of the string resource? For example, inserting "food_key", where food_key is the name of a string in your strings.xml files. They'd look something like this

/res/values/strings.xml:

<resources>
    <string name="food_key">Food</string>
    <string name="walk_key">Walk</string>
</resources>

and /res/values-pt/strings.xml

<resources>
    <string name="walk_key">Passeio</string>>
    <string name="food_key">Comida</string>
</resources>

Then, after getting something like "food" from a database query, you can get the actual string for display using something like this method:

  private String getStringResourceByName(String aString) {
    String packageName = getPackageName();
    int resId = getResources().getIdentifier(aString, "string", packageName);
    return getString(resId);
  }

This has the benefit of displaying the text to the user in whatever language their phone is currently using.

  1. I am proposing that the data you insert into your database isn't the English display values or the Portuguese display values. Instead, you insert the name of the resource. Then, instead of displaying the values stored in the database directly to the user, you use getStringResourceByName() to get the English or Pt translation at runtime. In this way, you don't need to update your database at all when the user changes the their device language.

I'm going to have handreds of values (in my case Category type). As you can see in code above the Categoty has more columns instead 'name' only.

If the categories are the same in English and Portuguese, you can store the display values directly in the database. If they're different depending on the language, then you'd need to do something similar by providing language-specific resources for those as well.

Community
  • 1
  • 1
Michiyo
  • 1,161
  • 1
  • 14
  • 33
  • I've updated my answer. I'm not sure what you mean by "I don't see how to use res, because of this I tried using this above class". Have you tried what I suggested? – Michiyo May 09 '16 at 21:04
  • Think I'm using the device in English, when I start the app the categories will be inserted on database as english vaues. So, I change the language to Portuguese, now I want to update these values to Portuguese. These values can be edited or removed. I want to change only originals categories. If user edit or remove any category I want to do nothing. Understand now? – Douglas Fornaro May 09 '16 at 22:41
  • Okay, yes, then I do understand what you're saying. I can't help you with that approach because I don't think it's very practical. I'm suggesting an alternative approach. I can't tell from your responses whether you don't understand what I proposed, or whether you are only interested in solving your problem using the method you described in your original question. – Michiyo May 10 '16 at 02:38