我有两项活动,我们将它们命名为活动A和活动B.
活动A包含EditText和RadioButton元素,用户必须选择这些元素并输入将在活动B中传递的内容,该活动将在其自己的文本视图中显示该文本。
这一切都像魅力一样,唯一的问题是当我尝试使用SharedPreferences保存数据时,我无法正常工作。尝试了我可以在互联网上找到的每个教程,但没有一个能够奏效。
无论如何,这里是活动A的代码:
public class StatistikeInputMain extends AppCompatActivity {
RadioGroup radioGrupa;
RadioButton imperial, metric;
EditText visinaCM, visinaFT, visinaINC, tezina , bodyfat, tdee, bmi, bmr, ffmi;
TextView rezultatTDEE, textVisinaCM, textVisinaFI, rezultatTezina, rezultatBMI, rezultatFFMI, rezultatBMR, rezultatBF;
Button spremiRezultat;
int onStartCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statistike_input_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState != null) {
String inputi = savedInstanceState.getString("INPUTI");
tdee.setText(inputi);
}
onStartCount = 1;
if (savedInstanceState == null) {
this.overridePendingTransition(R.anim.anim_slide_in_left,
R.anim.anim_slide_out_left);
} else {
onStartCount = 2;
}
// RADIO
radioGrupa = (RadioGroup) findViewById(R.id.radioGrupaStatistike);
imperial = (RadioButton) findViewById(R.id.radioStatistikeImperial);
metric = (RadioButton) findViewById(R.id.radioStatistikeMetric);
//VISINA
visinaCM = (EditText) findViewById(R.id.editTextStatistikeVisinaCM);
visinaFT = (EditText) findViewById(R.id.editTextStatistikeVisinaFEET);
visinaINC = (EditText) findViewById(R.id.editTextStatistikeVisinaINCH);
//TEŽINA
tezina = (EditText) findViewById(R.id.editTextStatistikeTezina);
//BODYFAT
bodyfat = (EditText) findViewById(R.id.editTextStatistikeBF);
//TDEE
tdee = (EditText) findViewById(R.id.editTextStatistikeTDEE);
//BMI
bmi = (EditText) findViewById(R.id.editTextStatistikeBMI);
//BMR
bmr = (EditText) findViewById(R.id.editTextStatistikeBMR);
//FFMI
ffmi = (EditText) findViewById(R.id.editTextStatistikeFFMI);
//BUTTON
spremiRezultat = (Button) findViewById(R.id.buttonStatistikeInputSpremi);
//REZULTATI
rezultatTDEE = (TextView) findViewById(R.id.textViewStatistikeRezultatTDEE);
textVisinaCM = (TextView) findViewById(R.id.textViewStatistikeRezultatVisina);
textVisinaFI = (TextView) findViewById(R.id.textViewStatistikeVisinaFeetInch);
rezultatTezina = (TextView) findViewById(R.id.textViewStatistikeRezultatTezina);
rezultatBMI = (TextView) findViewById(R.id.textViewStatistikeBMI);
rezultatBMR = (TextView) findViewById(R.id.textViewStatistikeBMR);
rezultatFFMI = (TextView) findViewById(R.id.textViewStatistikeFFMI);
rezultatBF = (TextView) findViewById(R.id.textViewStatistikeRezultatBodyFat);
}
public void radioStatistikeInputMetric(View view){
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.radioStatistikeMetric:
if (checked)
// TU OVO SREDI!!!
//textVisinaCM.setVisibility(View.VISIBLE);
// visinaCM.setVisibility(View.VISIBLE);
//textVisinaFI.setVisibility(View.GONE);
//visinaFT.setVisibility(View.GONE);
// visinaINC.setVisibility(View.GONE);
spremiRezultat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("tdeeInput", tdee.getText().toString());
intent.putExtra("bodyFatInput", bodyfat.getText().toString());
intent.putExtra("tezinaMetricInput", tezina.getText().toString());
intent.putExtra("visinaMetricInput", visinaCM.getText().toString());
intent.putExtra("bmiInput", bmi.getText().toString());
intent.putExtra("bmrInput", bmr.getText().toString());
intent.putExtra("ffmiInput", ffmi.getText().toString());
startActivity(intent);
}
});
break;
}
}
public void radioStatistikeInputImperial(View view){
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.radioStatistikeImperial:
if (checked)
//textVisinaCM.setVisibility(View.GONE);
// visinaCM.setVisibility(View.GONE);
// textVisinaFI.setVisibility(View.VISIBLE);
// visinaFT.setVisibility(View.VISIBLE);
// visinaINC.setVisibility(View.VISIBLE);
spremiRezultat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), AppLayoutMain.class);
intent.putExtra("tdeeInput", tdee.getText().toString());
intent.putExtra("bodyFatInput", bodyfat.getText().toString());
intent.putExtra("tezinaImperialInput", tezina.getText().toString());
intent.putExtra("visinaImperialInputFT", visinaFT.getText().toString());
intent.putExtra("visinaImperialInputINC", visinaINC.getText().toString());
intent.putExtra("bmiInput", bmi.getText().toString());
intent.putExtra("bmrInput", bmr.getText().toString());
intent.putExtra("ffmiInput", ffmi.getText().toString());
startActivity(intent);
}
});
break;
}
}
正如您所看到的,活动B中显示的结果将取决于用户选择,主要选择RadioButton(我认为存在问题)。
以下是活动B的代码:
public class AppLayoutMain extends AppCompatActivity {
private DrawerLayout mDrawer;
private ActionBarDrawerToggle drawerToggle;
private Toolbar toolbar;
int onStartCount = 0;
ProgressDialog progress;
NavigationView nvDrawer;
DrawerLayout dlDrawer;
android.support.v7.widget.CardView karticaStatistike;
TextView tdeeRezultat, bodyFatRezultat, bmiRezultat, bmrRezultat, ffmiRezultat, tezinaMetricRezultat, visinaMetricRezultat, tezinaImperialRezultat, visinaFTImperialRezultat, visinaINCImperialRezultat, ciljTreningRezultat, razinaTreningRezultat, preporukaTreningRezultat, oneRMPotisakSKlupeRezultat, oneRMCucanjRezultat, oneRMMrtvoDizanjeRezultat, ciljPrehranaRezultat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_layout_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
karticaStatistike = (android.support.v7.widget.CardView) findViewById(R.id.Statiske);
// STATISTIKE - PASSAN DATA
Intent intent = getIntent();
String tdee = intent.getStringExtra("tdeeInput");
String bf = intent.getStringExtra("bodyFatInput");
String bmi = intent.getStringExtra("bmiInput");
String bmr = intent.getStringExtra("bmrInput");
String ffmi = intent.getStringExtra("ffmiInput");
String tezinaMetric = intent.getStringExtra("tezinaMetricInput");
String visinaMetric = intent.getStringExtra("visinaMetricInput");
String tezinaImperial = intent.getStringExtra("tezinaImperialInput");
String visinaImperial1 = intent.getStringExtra("visinaImperialInputFT");
String visinaImperial2 = intent.getStringExtra("visinaImperialInputINC");
tdeeRezultat = (TextView) findViewById(R.id.textViewStatistikeRezultatTDEE);
tdeeRezultat.setText(tdee);
bodyFatRezultat = (TextView) findViewById(R.id.textViewStatistikeRezultatBodyFat);
bodyFatRezultat.setText(bf);
bmiRezultat = (TextView) findViewById(R.id.textViewStatistikeBMI);
bmiRezultat.setText(bmi);
bmrRezultat = (TextView) findViewById(R.id.textViewStatistikeBMR);
bmrRezultat.setText(bmr);
ffmiRezultat = (TextView) findViewById(R.id.textViewStatistikeFFMI);
ffmiRezultat.setText(ffmi);
tezinaMetricRezultat = (TextView) findViewById(R.id.textViewStatistikeRezultatTezina);
tezinaMetricRezultat.setText(tezinaMetric);
visinaMetricRezultat = (TextView) findViewById(R.id.textViewStatistikeRezultatVisina);
visinaMetricRezultat.setText(visinaMetric);
// SMISLI ŠTA ĆEŠ S OVIM, IZAZIVA KONFLIKT U VISINI I TEŽINI, PROBAJ S NEKIM IFOM
// tezinaImperialRezultat = (TextView) findViewById(R.id.textViewStatistikeRezultatTezina);
// tezinaImperialRezultat.setText(tezinaImperial);
//visinaFTImperialRezultat = (TextView) findViewById(R.id.textViewStatistikeRezultatVisina);
//visinaFTImperialRezultat.setText(visinaImperial1 + "'" + " " + visinaImperial2 + "''");
// STATISTIKE - KRAJ PASSANJA
// TRENING - PASSAN DATA
String oneRMBench = intent.getStringExtra("oneRMBenchInput");
String oneRMSquat = intent.getStringExtra("oneRMSquatInput");
String oneRMDead = intent.getStringExtra("oneRMDeadInput");
String ciljTreninga = intent.getStringExtra("vrijednostSpinneraCilj");
String razinaIskustva = intent.getStringExtra("vrijednostSpinneraRazinaIskustva");
oneRMPotisakSKlupeRezultat = (TextView) findViewById(R.id.textViewTreningRezultat1RMPotisakSKlupe);
oneRMPotisakSKlupeRezultat.setText(oneRMBench);
oneRMCucanjRezultat = (TextView) findViewById(R.id.textViewTreningRezultat1RMCucanj);
oneRMCucanjRezultat.setText(oneRMSquat);
oneRMMrtvoDizanjeRezultat = (TextView) findViewById(R.id.textViewTreningRezultat1RMMrtvoDizanje);
oneRMMrtvoDizanjeRezultat.setText(oneRMDead);
ciljTreningRezultat = (TextView) findViewById(R.id.textViewTreningRezultatCilj);
ciljTreningRezultat.setText(ciljTreninga);
razinaTreningRezultat = (TextView) findViewById(R.id.textViewTreningRezultatRazinaIskustva);
razinaTreningRezultat.setText(razinaIskustva);
// PREHRANA - PASSAN DATA
String prehrana = intent.getStringExtra("vrijednostSpinnera");
ciljPrehranaRezultat = (TextView) findViewById(R.id.textViewPrehranaCiljRezultat);
ciljPrehranaRezultat.setText(prehrana);
tdeeRezultat = (TextView) findViewById(R.id.textViewPrehranaTDEE);
tdeeRezultat.setText(tdee);
我没有在这里粘贴所有代码,只是相关部分。
我不知道该怎么做,我删除了所有与SharedPreferences有关的文本,因为它不起作用,所以如果有人能指引我完成这件事,我会很感激!!
我就像你说的那样做了,但仍然没有保存数据..这就是我所做的:
活动A:
SharedPreferences preference;
// IN ONCREATE
preference = getSharedPreferences("STATE", MODE_PRIVATE);
preference.edit().putString("tdeeInput", tdee.getText().toString()).commit();
活动B:
SharedPreferences preference;
// IN ONCREATE
preference = getSharedPreferences("STATE", Context.MODE_PRIVATE);
tdee = preference.getString("tdeeInput", null);
在活动B中,tdee
应保存结果ov tdeeRezultat
,您可以在我最初发布的活动B代码中找到该结果。无法将tdee
设置为tdeeRezultat
,因为它不兼容类型。这是问题吗?
答案 0 :(得分:1)
在onClick函数中添加
preference = getSharedPreferences("STATE", MODE_PRIVATE);
preference.edit().putString(key, value).commit();
在你的第二个活动中,只需致电
preference = getSharedPreferences("STATE",
Context.MODE_PRIVATE);
email = preference.getString("yourkey", null);
答案 1 :(得分:0)
- 已解决! -
活动A:
public static final String PREFS_NAME = "MyPreferencesFile";
//And in buttons onClick
spremiRezultat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("tdeeSave", tdee.getText().toString());
editor.putString("bodyfatSave", bodyfat.getText().toString());
editor.putString("tezinaMetricSave", tezina.getText().toString());
editor.putString("visinaMetricSave", visinaCM.getText().toString());
editor.putString("bmiSave", bmi.getText().toString());
editor.putString("bmrSave", bmr.getText().toString());
editor.putString("ffmiSave", ffmi.getText().toString());
editor.commit();
活动B:
public static final String PREFS_NAME = "MyPreferencesFile";
//In onCreate
tdeeRezultat = (TextView) findViewById(R.id.textViewStatistikeRezultatTDEE);
tdeeRezultat.setText(settings.getString("tdeeSave", "..."));
bodyFatRezultat = (TextView) findViewById(R.id.textViewStatistikeRezultatBodyFat);
bodyFatRezultat.setText(settings.getString("bodyfatSave", "..."));
bmiRezultat = (TextView) findViewById(R.id.textViewStatistikeBMI);
bmiRezultat.setText(settings.getString("bmiSave", "..."));
bmrRezultat = (TextView) findViewById(R.id.textViewStatistikeBMR);
bmrRezultat.setText(settings.getString("bmrSave", "..."));
ffmiRezultat = (TextView) findViewById(R.id.textViewStatistikeFFMI);
ffmiRezultat.setText(settings.getString("ffmiSave", "..."));
tezinaMetricRezultat = (TextView) findViewById(R.id.textViewStatistikeRezultatTezina);
tezinaMetricRezultat.setText(settings.getString("tezinaMetricSave", "..."));
visinaMetricRezultat = (TextView) findViewById(R.id.textViewStatistikeRezultatVisina);
visinaMetricRezultat.setText(settings.getString("visinaMetricSave", "..."));