Gson从共享偏好中读取数据

时间:2017-01-02 17:07:22

标签: java android json gson

我有一个问题,当我使用json将我的arraylist保存到共享首选项中并且我试图将其加载回我的listview时,listview不会改变。我读了很多文章,但每次都以同样的方式展示。你知道问题出在哪里吗?

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();
    }
});

2 个答案:

答案 0 :(得分:1)

在Global类中添加此方法,以便您可以在任何地方使用它。

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;
  }
保存按钮上的

单击此处调用保存方法:

Global.save_ListToSharedPreferneces(context, yourArrayList);

在你的加载按钮上单击只需调用get方法:

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

//Now set your adapter again:

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

答案 1 :(得分:0)

我通过预先处理结果并通过.add将它们添加到数组中来修复它。

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