如何使用共享首选项实现RecyclerView

时间:2018-11-27 11:58:07

标签: java android

为什么总是只在列表中添加一项? 如何实现共享首选项的recyclerview?

private void init() {
        RecyclerView recyclerView = findViewById(R.id.rvMaps);

        SharedPreferences prefs = getSharedPreferences("nameShared", Context.MODE_PRIVATE);
        String listJSON = prefs.getString("nameShared", "");
        String nameJ = prefs.getString("name", "");
        List<Marker> paramList =
                new Gson().fromJson(listJSON, new TypeToken<List<Marker>>() {
                }.getType());

        mapList.add(new Map(nameJ, paramList));

        mAdapter = new MapAdapter(mapList, new MapAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Map item) {
                Toast.makeText(MapListActivity.this, "Clicked: " + item.getName(), Toast.LENGTH_SHORT).show();
                Intent i = new Intent(MapListActivity.this, MainActivity.class);
                startActivity(i);
            }
        });

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(mAdapter);

        mAdapter.notifyDataSetChanged();

    }

0 个答案:

没有答案