Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:SharedReferences问题_Android_Android Sharedpreferences - Fatal编程技术网

Android:SharedReferences问题

Android:SharedReferences问题,android,android-sharedpreferences,Android,Android Sharedpreferences,嗨,伙计们,请看看这个 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_grade_viewer); getSupportActionBar().setDisplayHomeAsUpEnabled(true); myListView =

嗨,伙计们,请看看这个

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;
                }
            }
        });
    }
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u grade\u viewer);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
myListView=(ListView)findViewById(R.id.list);
字符串记录[]={“预科”、“期中”、“期末”、“期末成绩”};
myAdapter=new ArrayAdapter(this,R.layout.list_rec,R.id.tvRec,records);
myListView.setAdapter(myAdapter);
myListView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串项=((TextView)(view.findviewbyd(R.id.tvRec))).getText().toString();
SharedReferences首选项=获取SharedReferences(“MyApp”,模式\私人);
preferences.edit().putString(“术语”,术语);
开关(位置){
案例0:
意向意向=新意向(GradeViewer.this、PrelimGrade.class);
星触觉(意向);
打破
案例1:
Intent intent1=新的Intent(GradeViewer.this、MidtermGrade.class);
星触觉(intent1);
打破
案例2:
Intent intent2=新的Intent(GradeViewer.this,tentivefinalgrade.class);
星触觉(intent2);
打破
案例3:
Intent intent3=新的Intent(GradeViewer.this,FinalGrade.class);
起始触觉(intent3);
打破
}
}
});
}
我正在尝试将字符串(术语)传递给第二个活动。 例如,我按下了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;
            }

哦,糟了,是的,我完全忘了。如果你只是给活动传递了一些字符串,为什么不把字符串放在一个包中,并带着意图发送到下一个活动呢。意向。额外(“期限”,期限);星触觉(意向);谢谢你的回答,先生,但是上面的问题已经回答了