Question

In my app i am storing a 2 dimensional array using shared prefernces which contains dates now i have to match the array dates with current date the whole thing is on public void onrecieve()

At the time of saving it gives me no error but when i match current date with arrays date it always show false doesnt matter what is the date and it never goes in the if condition

String[][] my_date;
Set<String> checkDate;

/* getting today date and coverting it into string */
Calendar cal=Calendar.getInstance();
int todaysDate = cal.get(Calendar.DAY_OF_MONTH);
String cDate = Integer.toString(todaysDate);

SharedPreferences pref = arg0.getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
Set<String> set = new HashSet<String>();
for(int i = 0; i < my_date.length; i++){
    for(int j = 0; j < my_date[i].length; j++){
        set.add("'" + my_date[i][j] + "'"); 
        Log.v("dates", my_date[i][j]);
    }
}
editor.putStringSet("dates", set);
editor.commit();

checkDate = pref.getStringSet("dates", set);

if(checkDate.contains(cDate)){
    Intent mIntent = new Intent(arg0,DialogActivity.class); 
    mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    arg0.startActivity(mIntent);
}
Was it helpful?

Solution

Of course it will give you false always. You put day numbers to the set with apostrophes. So, your Set will have items like: '1', '2', '3'. Whilst String representation of cDate has no such apostrophes, so String.valueOf(1) is not equal "'1'". Try to replace this:

set.add("'" + my_date[i][j] + "'");

with this:

set.add(my_date[i][j]);

OTHER TIPS

you are adding a ' before and after your adding the String to the set. change this to the following to only store the date. Than your Strings should be equal:

set.add(my_date[i][j]);//instead of set.add("'" + my_date[i][j] + "'");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top