2013-01-24 13 views
8

मैं कक्षा ऑब्जेक्ट को एंड्रॉइड साझा वरीयता में स्टोर करना चाहता हूं। मैंने उस पर कुछ बुनियादी खोज की और मुझे कुछ जवाब मिल गए जैसे इसे धारावाहिक वस्तु बनाते हैं और इसे स्टोर करते हैं लेकिन मेरी ज़रूरत बहुत आसान है। मैं कुछ उपयोगकर्ता जानकारी जैसे नाम, पता, आयु और बूलियन मान को सक्रिय करना चाहता हूं सक्रिय है। मैंने इसके लिए एक उपयोगकर्ता वर्ग बनाया है।एंड्रॉइड साझा कक्षा में क्लास ऑब्जेक्ट को स्टोर करने के लिए कैसे करें?

public class User { 
    private String name; 
    private String address; 
    private int  age; 
    private boolean isActive; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    public boolean isActive() { 
     return isActive; 
    } 

    public void setActive(boolean isActive) { 
     this.isActive = isActive; 
    } 
} 

धन्यवाद। "नाम", "पता

+0

इसे धारावाहिक क्यों नहीं बनाते? यह सही समाधान है। – Simon

उत्तर

18
  1. डाउनलोड gson-1.7.1.jar: GsonLibJar

  2. अपने Android परियोजना के लिए इस पुस्तकालय जोड़ें और निर्माण पथ कॉन्फ़िगर करें।

  3. अपने पैकेज में निम्न श्रेणी जोड़ें।

    package com.abhan.objectinpreference; 
    
    import java.lang.reflect.Type; 
    import android.content.Context; 
    import android.content.SharedPreferences; 
    import com.google.gson.Gson; 
    import com.google.gson.reflect.TypeToken; 
    
    public class ComplexPreferences { 
        private static ComplexPreferences  complexPreferences; 
        private final Context     context; 
        private final SharedPreferences   preferences; 
        private final SharedPreferences.Editor editor; 
        private static Gson      GSON   = new Gson(); 
        Type         typeOfObject = new TypeToken<Object>(){} 
                       .getType(); 
    
    private ComplexPreferences(Context context, String namePreferences, int mode) { 
        this.context = context; 
        if (namePreferences == null || namePreferences.equals("")) { 
         namePreferences = "abhan"; 
        } 
        preferences = context.getSharedPreferences(namePreferences, mode); 
        editor = preferences.edit(); 
    } 
    
    public static ComplexPreferences getComplexPreferences(Context context, 
         String namePreferences, int mode) { 
        if (complexPreferences == null) { 
         complexPreferences = new ComplexPreferences(context, 
           namePreferences, mode); 
        } 
        return complexPreferences; 
    } 
    
    public void putObject(String key, Object object) { 
        if (object == null) { 
         throw new IllegalArgumentException("Object is null"); 
        } 
        if (key.equals("") || key == null) { 
         throw new IllegalArgumentException("Key is empty or null"); 
        } 
        editor.putString(key, GSON.toJson(object)); 
    } 
    
    public void commit() { 
        editor.commit(); 
    } 
    
    public <T> T getObject(String key, Class<T> a) { 
        String gson = preferences.getString(key, null); 
        if (gson == null) { 
         return null; 
        } 
        else { 
         try { 
          return GSON.fromJson(gson, a); 
         } 
         catch (Exception e) { 
          throw new IllegalArgumentException("Object stored with key " 
            + key + " is instance of other class"); 
         } 
        } 
    } } 
    
  4. इस

    package com.abhan.objectinpreference; 
    
    import android.app.Application; 
    
    public class ObjectPreference extends Application { 
        private static final String TAG = "ObjectPreference"; 
        private ComplexPreferences complexPrefenreces = null; 
    
    @Override 
    public void onCreate() { 
        super.onCreate(); 
        complexPrefenreces = ComplexPreferences.getComplexPreferences(getBaseContext(), "abhan", MODE_PRIVATE); 
        android.util.Log.i(TAG, "Preference Created."); 
    } 
    
    public ComplexPreferences getComplexPreference() { 
        if(complexPrefenreces != null) { 
         return complexPrefenreces; 
        } 
        return null; 
    } } 
    
  5. तरह Application वर्ग का विस्तार अपने प्रकट की application टैग इस तरह में है कि आवेदन वर्ग जोड़े द्वारा एक और वर्ग बनाएँ।

    <application android:name=".ObjectPreference" 
        android:allowBackup="false" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" > 
    ....your activities and the rest goes here 
    </application> 
    
  6. आपका मुख्य गतिविधि जहां Shared Preference में दुकान मूल्य करना चाहता था कुछ इस तरह करते हैं में

    package com.abhan.objectinpreference; 
    
    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.view.View; 
    
    public class MainActivity extends Activity { 
        private final String TAG = "MainActivity"; 
        private ObjectPreference objectPreference; 
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
    
        objectPreference = (ObjectPreference) this.getApplication(); 
    
        User user = new User(); 
        user.setName("abhan"); 
        user.setAddress("Mumbai"); 
        user.setAge(25); 
        user.setActive(true); 
    
        User user1 = new User(); 
        user1.setName("Harry"); 
        user.setAddress("London"); 
        user1.setAge(21); 
        user1.setActive(false); 
    
        ComplexPreferences complexPrefenreces = objectPreference.getComplexPreference(); 
        if(complexPrefenreces != null) { 
         complexPrefenreces.putObject("user", user); 
         complexPrefenreces.putObject("user1", user1); 
         complexPrefenreces.commit(); 
        } else { 
         android.util.Log.e(TAG, "Preference is null"); 
        } 
    } 
    
    } 
    
  7. किसी अन्य गतिविधि जहां मूल्य Preference से कुछ इस तरह कर प्राप्त करना चाहता था में।

    package com.abhan.objectinpreference; 
    
    import android.app.Activity; 
    import android.os.Bundle; 
    
    public class SecondActivity extends Activity { 
        private final String TAG = "SecondActivity"; 
        private ObjectPreference objectPreference; 
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_second); 
    
        objectPreference = (ObjectPreference) this.getApplication(); 
        ComplexPreferences complexPreferences = objectPreference.getComplexPreference(); 
    
        android.util.Log.i(TAG, "User"); 
        User user = complexPreferences.getObject("user", User.class); 
        android.util.Log.i(TAG, "Name " + user.getName()); 
        android.util.Log.i(TAG, "Address " + user.getAddress()); 
        android.util.Log.i(TAG, "Age " + user.getAge()); 
        android.util.Log.i(TAG, "isActive " + user.isActive()); 
        android.util.Log.i(TAG, "User1"); 
        User user1 = complexPreferences.getObject("user", User.class); 
        android.util.Log.i(TAG, "Name " + user1.getName()); 
        android.util.Log.i(TAG, "Address " + user1.getAddress()); 
        android.util.Log.i(TAG, "Age " + user1.getAge()); 
        android.util.Log.i(TAG, "isActive " + user1.isActive()); 
    } } 
    

आशा यह आप कर सकते हैं। इस जवाब में मैंने संदर्भ 'उपयोगकर्ता' के लिए अपनी कक्षा का उपयोग किया ताकि आप बेहतर ढंग से समझ सकें। हालांकि, अगर आप प्राथमिकता में बहुत बड़ी वस्तुओं को स्टोर करने का विकल्प चुनते हैं तो हम इस विधि पर रिले नहीं कर सकते क्योंकि हम सभी जानते हैं कि हमारे पास डेटा निर्देशिका में प्रत्येक ऐप के लिए सीमित स्मृति आकार है ताकि यदि आप सुनिश्चित हैं कि आपके पास साझा वरीयता में स्टोर करने के लिए केवल सीमित डेटा है आप इस विकल्प का उपयोग कर सकते हैं।

इस कार्यान्वयन पर किसी भी सुझाव का स्वागत है।

+0

यदि मैं इसे –

+1

@PankajNimgade का उपयोग करके किसी प्रकार की ऐरेलिस्ट के रूप में सहेज रहा हूं, तो मैं ऑब्जेक्ट के रूप में सरणीसूची कैसे प्राप्त कर सकता हूं, आप ऑब्जेक्ट पर अपनी सरणी सूची सेट कर सकते हैं लेकिन सुनिश्चित करें कि आप अपनी ऑब्जेक्ट को सरणीकृत में क्रमबद्ध कर सकते हैं। इसके अलावा, आप इसे पार्सलबल बना सकते हैं और इसे सीधे इरादे से पास कर सकते हैं। –

+0

आपको बहुत बहुत धन्यवाद :) –

1

अन्य तरीके से itself..Preferences केवल आदिम प्रकार स्वीकार करके प्रत्येक संपत्ति को बचाने के लिए है, तो आप उस में एक जटिल वस्तु नहीं डाल सकते

0

तुम बस कुछ सामान्य SharedPreferences जोड़ सकता है "," उम्र "&" isActive "और बस उन्हें जब वर्ग

0

instantiating लोड आप वैश्विक वर्ग

public class GlobalState extends Application 
     { 
    private String testMe; 

    public String getTestMe() { 
     return testMe; 
     } 
    public void setTestMe(String testMe) { 
    this.testMe = testMe; 
    } 
} 

उपयोग कर सकते हैं और फिर nadroid menifest में आपके आवेदन टैग का पता लगाएँ, और इसे में जोड़ें:

android:name="com.package.classname" 

और आप निम्न कोड का उपयोग करके अपनी किसी भी गतिविधि से डेटा सेट और प्राप्त कर सकते हैं। इस लिंक से

 GlobalState gs = (GlobalState) getApplication(); 
    gs.setTestMe("Some String");</code> 

     // Get values 
    GlobalState gs = (GlobalState) getApplication(); 
    String s = gs.getTestMe();  
0

साझा किए गए संदर्भों द्वारा लॉगिन मूल्य को संग्रहीत करने का सरल समाधान।

आप मुख्य गतिविधि वर्ग या अन्य कक्षा का विस्तार कर सकते हैं जहां आप "उस चीज़ का मूल्य" स्टोर करेंगे जिसे आप रखना चाहते हैं "। इसे लेखक और पाठक वर्गों में रखें:

public static final String GAME_PREFERENCES_LOGIN = "Login"; 

यहां इनपुट क्लास इनपुट है और आउटपुट क्लास क्रमशः आउटपुट क्लास है।

// This is a storage, put this in a class which you can extend or in both classes: 
//(input and output) 
public static final String GAME_PREFERENCES_LOGIN = "Login"; 

// String from the text input (can be from anywhere) 
String login = inputLogin.getText().toString(); 

// then to add a value in InputCalss "SAVE", 
SharedPreferences example = getSharedPreferences(GAME_PREFERENCES_LOGIN, 0); 
Editor editor = example.edit(); 
editor.putString("value", login); 
editor.commit(); 

अब आप इसे अन्य कक्षा की तरह कहीं और उपयोग कर सकते हैं। निम्नलिखित आउटपुट क्लास है।

SharedPreferences example = getSharedPreferences(GAME_PREFERENCES_LOGIN, 0); 
String userString = example.getString("value", "defValue"); 

// the following will print it out in console 
Logger.getLogger("Name of a OutputClass".class.getName()).log(Level.INFO, userString); 
संबंधित मुद्दे

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