user5307594
user5307594

Reputation:

Gson reading data from shared preferences

I have a problem, that when I save my arraylist into shared preferences using json and I'm trying to load it back into my listview, the listview doesn't change. I have read a lot of articles, but they show it the same way every time. Do you have any idea where might be the problem?

save.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(thisView.getContext());
        SharedPreferences.Editor editor = sharedPrefs.edit();
        Gson gson = new Gson();

        String json = gson.toJson(mainActivity.getResultsArray());

        editor.putString("ResultsArray", json);
        editor.commit();
        Toast saved = Toast.makeText(getContext(), "Uloženo", Toast.LENGTH_LONG);
        saved.show();
    }
});

load.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(thisView.getContext());
        Gson gson = new Gson();
        String json = sharedPrefs.getString("ResultsArray", "");
        Type type = new TypeToken<ArrayList<Result>>(){}.getType();
        ArrayList<Result> results = gson.fromJson(json, type);
        mainActivity.setResultsArray(results);
        mainActivity.getAdapter().notifyDataSetChanged();
        Toast loaded = Toast.makeText(getContext(), "Načteno", Toast.LENGTH_LONG);
        loaded.show();
    }
});

Upvotes: 0

Views: 450

Answers (2)

Bhoomika Patel
Bhoomika Patel

Reputation: 1925

add this methods in your Global class, so you can use this anywhere.

public static void save_ListToSharedPreferneces(Context context, ArrayList<YourModelClass> recentDataList) {
    SharedPreferences sharedpreferences =
            context.getSharedPreferences("recent_data", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpreferences.edit();
    Gson gson = new Gson();

    String json = gson.toJson(recentDataList);
    editor.putString("recentDataList", json);
    editor.commit();
  }

  public static ArrayList<YourModelClass> get_ListFromSharedPreferneces(Context context) {

    SharedPreferences sharedPrefs = context.getSharedPreferences("recent_data", Context.MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPrefs.getString("recentDataList", "");
    Type type = new TypeToken<ArrayList<YourModelClass>>() {
    }.getType();

    ArrayList<YourModelClass> recentDataList= gson.fromJson(json, type);

    return recentDataList;
  }

on your save button click just call save method like this:

Global.save_ListToSharedPreferneces(context, yourArrayList);

on your load button click just call get method like this:

ArrayList<YourModelClass> yourArrayList=new ArrayList();
yourArrayList = Global.save_ListToSharedPreferneces(context);

//Now set your adapter again:

YourAdapter yourAdapter = new YourAdapter (yourArrayList , getContext());
listview.setAdapter(yourAdapter );

Upvotes: 2

user5307594
user5307594

Reputation:

I fixed it by foreaching results and adding them into array by .add.

        ArrayList<Result> results = gson.fromJson(json, type);
        for (Result result: results) {
            mainActivity.getResultsArray().add(result);
        }
        mainActivity.getAdapter().notifyDataSetChanged();

Upvotes: 0

Related Questions