0

I want to save Arraylist of Custom type Object in sharedPreference. For that purpose I passed Arraylist to Gson, there is no compile error but after running this code device wait for long time and got stuck. Kindly suggest me other way if I am on wrong way.

public void storeApps(Context context, List<AppInfo> apps) {
    SharedPreferences settings;
    SharedPreferences.Editor editor;
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    editor = settings.edit();
    Gson gson = new Gson();
    String jsonData = gson.toJson(apps);// this line wait for long time at compile time.
    editor.putString(Fav, jsonData);
    editor.commit();
}

AppInfo Class

public class AppInfo {
    public String appname = "";
    public String pname = "";
    public String versionName = "";
    public int versionCode = 0;
    public Drawable icon;
    public int color = 0;

    public String getAppname() {
        return appname;
    }

    public String getVersionName() {
        return versionName;
    }

    public String getPname() {
        return pname;
    }

    public Drawable getIcon() {
        return icon;
    }

    public int getVersionCode() {
        return versionCode;
    }

    public int getColor() {
        return color;
    }

    public void setAppname(String appname) {
        this.appname = appname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }

    public void setIcon(Drawable icon) {
        this.icon = icon;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public AppInfo() { }

    public AppInfo(String appname, String pname, String versionName, int versionCode, Drawable icon, int color) {
        this.appname = appname;
        this.pname = pname;
        this.versionName = versionName;
        this.versionCode = versionCode;
        this.icon = icon;
        this.color = color;
    }
}
17
  • Actualy app info is predefined class that is why the Gson is unable to convert it.You need to ceate your own class store the needed data only the field you want. Commented Dec 23, 2015 at 12:08
  • @Rohit Heera AppInfo is a class that i have created. It is not predefined class Commented Dec 23, 2015 at 12:23
  • Ok what is the size of the list Commented Dec 23, 2015 at 12:28
  • @RohitHeera apps.size() which is 29 Commented Dec 23, 2015 at 12:58
  • I can post a code it works fine in my case when i can convert the contact list.. Commented Dec 23, 2015 at 13:14

1 Answer 1

1
class Mydata 

{

ArrayList<AppInfo> data;

    public Mydata (List<AppInfo> data) {

        this.data= data;

    }

    public List<AppInfo> getContactSyn() {
        return data;
    }

    public void setContactSyn(List<AppInfo> data) {
        this.data= data;
    }


}

 Gson gson = new Gson();
MyData datalist = new MyData(data);

    String jsonData = gson.toJson(datalist);
Sign up to request clarification or add additional context in comments.

6 Comments

Actually you can covert list it is good practise to convert asingle object rather than a list of object
same problem again here
@njzk2 I think source is ok.
@Tarikhelian I am asking if Rohit has any source to support that claim [that converting a single object is "good practice"]
I have applied Rohit source as well but same result.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.