如何在android中使用共享首选项更新list recyclerview?

mrwjdhj3  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(305)

我正在使用共享首选项将数据从itemdetails类发送到ordersfragment。我成功地接收到数据,但只是最后一个项目,它正在覆盖购物车中的项目。如何用新值更新从itemdetails发送的首选项?
这是我的itemdetails类:

btn_AddToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
                Gson gson = new Gson();

                // send names
                ArrayList<String> namesList = new ArrayList<>();
                namesList.add(name);
                String namesJson = gson.toJson(namesList);
                sharedPreferences.edit().putString("names", namesJson).apply();

                // send prices
                ArrayList<String> pricesList = new ArrayList<>();
                pricesList.add(price);
                String pricesJson = gson.toJson(pricesList);
                sharedPreferences.edit().putString("prices", pricesJson).apply();

            }

        });

这是ordersfragment:

SharedPreferences sharedPreferences = context.getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
        Gson gson = new Gson();

        // get names
        ArrayList<String> namesList = new ArrayList<>();
        String namesJson = sharedPreferences.getString("names", null);
        String[] names = gson.fromJson(namesJson, String[].class);
        if (names != null) {
            namesList.clear();
            namesList.addAll(Arrays.asList(names));
        }

        // get prices
        ArrayList<String> pricesList = new ArrayList<>();
        String pricesJson = sharedPreferences.getString("prices", null);
        String[] prices = gson.fromJson(pricesJson, String[].class);
        if (prices != null) {
            pricesList.clear();
            pricesList.addAll(Arrays.asList(prices));
        }

        RecyclerView rvOrdered = view.findViewById(R.id.rv_ordered);
        rvOrdered.setLayoutManager(new LinearLayoutManager(context));
        rvOrdered.setHasFixedSize(true);

        //populate the orders list
        ArrayList<OrderedModel> orders = new ArrayList<>();

            for(int i = 0; i < namesList.size(); i++) {
                orders.add(new OrderedModel(namesList.get(i), pricesList.get(i)));
            }

        rvOrdered.setAdapter(new OrderedRecyclerViewAdapter(context, orders));

如何更新itemdetails中的列表并获取ordersfragment中的所有订单,而不是一个订单?我可以使用多个共享首选项吗?我想我必须先从片段中获取数据,然后再添加新的值?

v7pvogib

v7pvogib1#

首先使用以下方法获取SharedReference:

SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String namesJson = sharedPreferences.getString("names", "");
String[] names = gson.fromJson(namesJson, String[].class);

然后创建一个新数组,将所有旧名称加上新名称:

ArrayList<String> namesArray = new ArrayList<>();
for(String n : names) {
    namesArray.add(n);
}
namesArray.add(name); // add the new name here

然后在发送之前再次保存在itemdetails中:

sharedPreferences.edit().putString("names", gson.toJson(namesArray)).apply();

现在,您可以访问ordersfragment中的所有名称,新旧名称。对“价格”做同样的处理,效果也是一样的。

相关问题