skip to Main Content

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

    }
}

3

Answers


  1. It’s Easy just

    Resources res = getResources();
    String[] mornings = res.getStringArray(R.array.morning);
    
    //then use mornings  as you use array.
    
    
    Login or Signup to reply.
  2. String morning[] = getResources().getStringArray(R.array.morning);
    
    Login or Signup to reply.
  3.     // Solution
        SharedPreferences sharedPreferences = getSharedPreferences("my_preference", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
    
        String currentDate = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()).format(System.currentTimeMillis());
        String[] msgArrays = getResources().getStringArray(R.array.morning);
    
        String savedDate = sharedPreferences.getString(DATE_KEY, "");
        int savedCounter = sharedPreferences.getInt(COUNTER_KEY, 0);
    
    
        // First time
        if (savedDate.isEmpty()) {
            editor.putInt(COUNTER_KEY, savedCounter);
            editor.putString(DATE_KEY, currentDate);
            editor.apply();
        } else if (!savedDate.equals(currentDate)) {
            // New date appears, update shared preference.
            savedCounter++;
            if (savedCounter < msgArrays.length) {
                editor.putInt(COUNTER_KEY, savedCounter);
                editor.putString(DATE_KEY, currentDate);
                editor.apply();
            }
        }
        binding.tvTitle.setText(msgArrays[savedCounter]);
    

    In this snipped of code, I’m updating shared preference by date and counter.

    Date: It will be saved for next date, and if these both are not matching, shared preference will gets update with counter.

    Counter: The position of your strings-arrays.

    Reset: I have add another check that if in case your counter reaches to the end of array, it will be reset to ‘0’.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search