سؤال

I am saving "long" in Sharedpreferences as below :

SharedPreferences preferences = context.getSharedPreferences("STARTTIME", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putLong("startTime", startTime);
    editor.commit();

and Retreiving "long" from Sharedpreference as below :

preferences = context.getSharedPreferences("STARTTIME", android.content.Context.MODE_PRIVATE);
long getstartTime = preferences.getLong(startTime, 0);

But I am getting value "0" while retreiving.....Any guess where am i making mistake ?

هل كانت مفيدة؟

المحلول

This:

long getstartTime = preferences.getLong(startTime, 0);

should be

long getstartTime = preferences.getLong("startTime", 0);

Android interpreted the startTime as a resource id, and because it didn't find it, you get the default value, that you passed in, as returnvalue.

نصائح أخرى

You should pass the same key to the getLong() method i.e. literal "startTime" .

long getstartTime = preferences.getLong("startTime", 0);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top