Comparing selected day with actual day

Put your problem here if it does not fit any of the other categories.

Comparing selected day with actual day

Postby la3bna » Tue Sep 06, 2011 12:51 pm

Hi

Im trying to use shared prefs to have a notification appear on the weekday the user sets in the prefs. I have already set up the alarmmanger to trigger the notification service at a set interval.

Now Im trying to get the selected day preference to compare to the actual day witch is read from calendar like this.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate() {
  2.                 super.onCreate();
  3.  
  4.                
  5.                 nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  6.                  Calendar cal = Calendar.getInstance();
  7.               idag = cal.get(Calendar.DAY_OF_WEEK);
  8.                 SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  9.                 valgtdag = sharedPrefs.getString("valgt_dag", "1");
  10.                 dag = getString(idag);
  11.                
  12.                
  13.                
  14.                 if( dag == valgtdag)
  15.                 {              
  16.                 showNotification();
  17.                 }
  18.                 else
  19.                 {
  20.                        
  21.                 }
  22.        
  23.        
  24.         }
  25.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Im getting error like this in DDMS:

String resource ID #0x3

Can anyone tell me what I have done wrong??

Thanks in advace
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

Top

Re: Comparing selected day with actual day

Postby Aseriesofsmallprojects » Wed Sep 07, 2011 12:05 am

The first problem is that you're comparing two strings with "==". You want to use something more like
if (dag.equals(valgtdag))

Your error is caused by getString(idag); This function is supposed to be given a resource ID, not data from your program. It doesn't convert a numerical value to a string for the name of the day.
www.ProjectJourneyman.com - Android development and app marketing
twitter.com/PrjJourneyman Twitter feed
Android Developer Tutorial A growing tutorial for Android Devs
The Android Income Series Books on income topics for Android developers
Aseriesofsmallprojects
Developer
Developer
 
Posts: 39
Joined: Fri Oct 08, 2010 4:29 pm
Location: USA

Re: Comparing selected day with actual day

Postby la3bna » Thu Sep 08, 2011 6:08 am

So basically Im using the getString wrong.

But how do I make the calendar week day value in to a string for comparing?
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

Re: Comparing selected day with actual day

Postby jonbonazza » Thu Sep 08, 2011 10:11 pm

change
Code: Select all
dag = getString(idag);

to
Code: Select all
dag = Integer.toString(idag);


and use .equals() for comparing strings instead of '=='.

For an explanation of the latter, see here:
http://www.bonafide-software.com/blog/2011/08/31/12/
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Comparing selected day with actual day

Postby la3bna » Fri Sep 09, 2011 5:40 am

Thanks a lot.. I will try that..
Edit
That did the trick for me..
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 34 guests