user3660378
user3660378

Reputation:

Android: SharedPreferences Issue

Hi guys please check this out

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grade_viewer);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        myListView = (ListView) findViewById(R.id.list);

        String records[] = {"Prelim","Midterm","Final", "Final Grade"};

        myAdapter = new ArrayAdapter<String>(this, R.layout.list_rec, R.id.tvRec, records);
        myListView.setAdapter(myAdapter);
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String TERM = ((TextView) (view.findViewById(R.id.tvRec))).getText().toString();
                SharedPreferences preferences = getSharedPreferences("MyApp", MODE_PRIVATE);
                preferences.edit().putString("term", TERM);
                switch (position){
                    case 0:
                        Intent intent = new Intent(GradeViewer.this, PrelimGrade.class);
                        startActivity(intent);
                        break;
                    case 1:
                        Intent intent1 = new Intent(GradeViewer.this, MidtermGrade.class);
                        startActivity(intent1);
                        break;
                    case 2:
                        Intent intent2 = new Intent(GradeViewer.this, TentativeFinalGrade.class);
                        startActivity(intent2);
                        break;
                    case 3:
                        Intent intent3 = new Intent(GradeViewer.this, FinalGrade.class);
                        startActivity(intent3);
                        break;
                }
            }
        });
    }

I am trying to pass the string(term) to second activity. for example I pressed the 1st Item in ListView which is (Prelim) then the shared preferences will pass the string in the second activity.

And the problem is when I pressed the 2nd(Midterm), 3rd(Final) item etc. etc. the string that stored in shared preferences is still (Prelim).

Upvotes: 1

Views: 85

Answers (2)

J&#246;rn Buitink
J&#246;rn Buitink

Reputation: 2916

You forgot to apply or save your changes to shared preferences:

            String TERM = ((TextView) (view.findViewById(R.id.tvRec))).getText().toString();
            SharedPreferences preferences = getSharedPreferences("MyApp", MODE_PRIVATE);

            // here you should apply or commit your changes:
            preferences.edit().putString("term", TERM).apply(); 
            switch (position){
                case 0:
                    Intent intent = new Intent(GradeViewer.this, PrelimGrade.class);

                    // if you only need your "TERM" in the next activity, it would be a cleaner
                    // approach to send it via your intent:
                    intent.putExtra(TERM, "term");

                    startActivity(intent);
                    break;
                case 1:
                    Intent intent1 = new Intent(GradeViewer.this, MidtermGrade.class);
                    startActivity(intent1);
                    break;
                case 2:
                    Intent intent2 = new Intent(GradeViewer.this, TentativeFinalGrade.class);
                    startActivity(intent2);
                    break;
                case 3:
                    Intent intent3 = new Intent(GradeViewer.this, FinalGrade.class);
                    startActivity(intent3);
                    break;
            }

Upvotes: 0

nano_nano
nano_nano

Reputation: 12523

I think you forgott something like

preferences.edit().putString("term", TERM).commit();

Upvotes: 3

Related Questions