Click here to Skip to main content
15,889,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have small app should pass values from 1 activity to custom listview that display in tab. The problem is I have 4 tabs and the values should appears in the 2nd tab but it doesn't, only after I pressed the last tab (4th) and go back to the 2nd tab the values diaplay. I want them appears immidietly after I pressed the button. What am I doing wrong ? thanks

What I have tried:

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900