Home > database >  How to get values from array in strings.xml in order of each day?
How to get values from array in strings.xml in order of each day?

Time:11-21

I want to get values ​​from an array every day, respectively, and set these values ​​to a textbox. I can get values ​​from array and set them in textbox but next day value stays same. does not set the next value in array? what can I do?

<string-array name="morning">
    <item>Good Morning. Message 1.</item>
    <item>Good Morning. Message 2.</item>
    <item>Good Morning. Message 3.</item>
</string-array>
mTestArray = getResources().getStringArray(R.array.morning);

preference_shared = this.getSharedPreferences("PREFERENCE", MODE_PRIVATE);
text_shared = this.getSharedPreferences("TEXT", MODE_PRIVATE);

Calendar c = Calendar.getInstance();
int timeOfDay = c.get(Calendar.DAY_OF_YEAR);

if (timeOfDay >= 0 && timeOfDay < 24) {
    if (preference_shared.getBoolean("isFirstRun", true)) {
        dailyGreetings.setText(mTestArray[(0) % (mTestArray.length)]);
        saveDate();
    }else {
        if (!Objects.equals(preference_shared.getString("Date", ""), dateFormat.format(date))) {
            int idx = new Random().nextInt(mTestArray.length);
            dailyGreetings.setText(mTestArray[idx]);
            text_shared.edit().putString("TEXT", dailyGreetings.getText().toString()).apply();
            saveDate();
        }
        else {
            dailyGreetings.setText(text_shared.getString("TEXT", ""));
        }

    }
}

CodePudding user response:

It's Easy just

Resources res = getResources();
String[] mornings = res.getStringArray(R.array.morning);

//then use mornings  as you use array.

CodePudding user response:

String morning[] = getResources().getStringArray(R.array.morning);
  • Related