簡體   English   中英

Android:SharedPreferences問題

[英]Android: SharedPreferences Issue

嗨,大家好,請檢查一下

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

我試圖將字符串(term)傳遞給第二個活動。 例如,我在ListView中按下第一個項目(Prelim),然后共享首選項將在第二個活動中傳遞字符串。

問題是當我按下第二個(中期),第三個(最終)項目等時,存儲在共享首選項中的字符串仍然是(Prelim)。

我覺得你忘記了類似的東西

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

您忘記將更改應用或保存到共享首選項:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM