2010-10-31 16 views
9

एंड्रॉइड दुनिया में नया होना और दिन में खुशी के साथ आगे बढ़ना;) मैं सामान्य उपयोग के बारे में उदाहरण साझा करना चाहता हूं।अधिक सामान्य तरीके से, ShareStreferences को स्थानीय स्टोर के रूप में कैसे उपयोग करें?

जेनेरिक लोकलस्टोर क्लास के साथ साझा किए गए संदर्भों का उपयोग करने के बारे में यहां उदाहरण दिया गया है।

अपनी मुख्य गतिविधि या किसी भी उप-गतिविधि द्वारा उपयोग की जाने वाली एक सामान्य कक्षा बनाएं।

public class LocalStore { 

     private static final String TAG = "LocalStore"; 
     private static final String PREF_FILE_NAME = "userprefs"; 

     public static void clear(Context context) { 
      clear(context, "unknown"); 
     } 
     public static void clear(Context context, String caller) { 
      Editor editor = 
       context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); 
      editor.clear(); 
      editor.commit(); 
      Log.d(TAG, "caller:"+caller + "|clear LocalStore"); 
     } 

     public static boolean setCustomBooleanData(String key, boolean value, Context context) { 
     Editor editor = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); 
     editor.putBoolean(key, value); 

     return editor.commit(); 
    } 
    public static boolean getCustomBooleanData(String key, Context context) { 
     SharedPreferences savedSession = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 

     return (savedSession.getBoolean(key, false)); 
    } 

    public static boolean setCustomStringData(String key, String value, Context context) { 
     Editor editor = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); 
     editor.putString(key, value); 

     return editor.commit(); 
    } 
    public static String getCustomStringData(String key, Context context) { 
     SharedPreferences savedSession = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 

     return (savedSession.getString(key, null)); 
    } 


    public static boolean isCustomStringExistInLocal(String customKey, Context context) { 
     SharedPreferences savedSession = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 

     return (savedSession.getString(customKey, null))==null?false:true; 
    } 

public static boolean saveObject(String objKey, Serializable dataObj, Context context) { 
     Editor editor = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); 
     editor.putString(objKey, ObjectSerializer.serialize(dataObj)); 

     Log.d(TAG, "savedObject| objKey:"+objKey+"/" + dataObj.toString()); 

     return editor.commit(); 
    } 
    public static Object getObject(String objKey, Context context) { 
     SharedPreferences savedSession = 
      context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 

     Object dataObj = ObjectSerializer.deserialize(savedSession.getString(objKey, null)); 

     return dataObj; 
    } 

    } 

ध्यान दें: आप here

का आनंद लें से ObjectSerializer उपयोग कर सकते हैं!

अतिरिक्त अद्यतन: मैं से
एक पुस्तकालय GENERIC_STORE रूप MEMDISKCACHE और SHAREDPREF उपयोग करने के लिए किसी को दिलचस्पी के लिए इसका इस्तेमाल कर सकते हैं कार्यान्वित ->https://github.com/wareninja/generic-store-for-android

+13

StackOverflow पूछे जाने वाले प्रश्न का कहना है::

public static boolean setData(Context, String key, boolean value) { Editor editor = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); editor.putBoolean(key, value); return editor.commit(); } public static boolean setData(Context, String key, String value) { Editor editor = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit(); editor.putString(key, value); return editor.commit(); } 

तो आप बस इस तरह अतिभारित कार्यों कॉल कर सकते हैं "यह भी पूछने के लिए और अपने खुद के सवाल का जवाब देने को पूरी तरह से ठीक है, जब तक आप नाटक करते हैं कि आप खतरे में हैं: इसे एक प्रश्न के रूप में वाक्यांश दें। " –

+0

क्या आप इसे प्रश्नपत्र में होने वाली हर चीज के बजाय उचित प्रश्न और उत्तर दे सकते हैं। अन्यथा यह हटा दिया जा सकता है। – Kev

+2

यह साझा करना आपके लिए अच्छा है :), लेकिन मुझे लगता है कि यह एक प्रश्न और उत्तर साइट के बजाय ब्लॉग में है। –

उत्तर

2

मान लिया जाये कि आप कैसे यह और भी बेहतर बनाने के लिए कुछ सुझाव दिए गए हैं, तो यहाँ आप चले जाओ।

  • आमतौर पर एंड्रॉइड Context चर को पहले पैरामीटर के रूप में रखने के लिए एक सम्मेलन का पालन करता है। किसी भी सामान्य पुस्तकालय बनाने के दौरान ऐसा करने के लिए यह अच्छा अभ्यास है।
  • यदि आप इसे अधिक सामान्य बनाना चाहते हैं, तो विधि अधिभार को क्यों न करें? यह Intent कक्षा में कितने एक्स्ट्रा को संभाला जाता है, इस तरह मूल्यों को सेट करते समय डेवलपर को अधिक लचीलापन देगा।

उदाहरण के लिए:

setData(this, "myBoolean", true); 
setData(this, "myString", "Its Awesome"); 
+1

धन्यवाद! अच्छा उत्तर – user1755546

संबंधित मुद्दे