activity findViewById(R.id.plusB).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (plus.getText().toString().trim().isEmpty() || tvDate.getText().toString().trim().isEmpty() || catEt.getText().toString().trim().isEmpty()) { Toast.makeText(getApplicationContext(), "not good", Toast.LENGTH_SHORT).show(); } else { final TextView line1 = findViewById(R.id.catEt); final EditText lineimg = findViewById(R.id.minusNum); final TextView line2 = findViewById(R.id.tvDate); String lin1 = line1.getText().toString(); String lin2 = line2.getText().toString(); String lin3 = lineimg.getText().toString(); mExampleList.add(new exampleItem(lin3, lin2, lin1)); mAdapter.notifyItemInserted(mExampleList.size()); mAdapter.notifyDataSetChanged(); saveData(); Intent intent = new Intent(MainActivity.this, tab2.class); startActivity(intent); finish(); } } }); } private void loadData() { SharedPreferences sharedPreferences = getSharedPreferences("shared preferences11", MODE_PRIVATE); Gson gson = new Gson(); String json = sharedPreferences.getString("task list11", null); Type type = new TypeToken<arraylist<exampleitem>>() { }.getType(); mExampleList = gson.fromJson(json, type); if (mExampleList == null) { mExampleList = new ArrayList<>(); } mAdapter.notifyDataSetChanged(); } private void saveData() { SharedPreferences sharedPreferences = getSharedPreferences("shared preferences11", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); Gson gson = new Gson(); String json = gson.toJson(mExampleList); editor.putString("task list11", json); editor.apply(); } } tab2 buildRecyclerView(v); loadData(); return v; } private void loadData() { SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("shared preferences11", Context.MODE_PRIVATE); Gson gson = new Gson(); String json = sharedPreferences.getString("task list11", null); Type type = new TypeToken<arraylist<exampleitem>>() { }.getType(); mExampleList = gson.fromJson(json, type); if (mExampleList == null) { mExampleList = new ArrayList<>(); } mAdapter.notifyDataSetChanged(); } private void buildRecyclerView(View view) { mRecyclerView = view.findViewById(R.id.recyclerview); mRecyclerView.setHasFixedSize(true); mLayoutManager = new LinearLayoutManager(getContext()); mAdapter = new exampleAdapter(mExampleList); mRecyclerView.setLayoutManager(mLayoutManager); mAdapter.notifyDataSetChanged(); mRecyclerView.setAdapter(mAdapter); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)