我在SharedPreferences函数中更改数据,当我在同一活动中调用时,数据是最新的,但是当交换活动不是最新时。
我使用此功能更新数据:
public static String setVal(Context context, String val){
String LOGIN = "Login";
SharedPreferences pref = context.getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
SharedPreferences.Editor editor2 = pref.edit();
editor2.remove("creditiApp");
editor2.putString("creditiApp",val);
editor2.apply();
editor2.commit();
String LOGIN2 = "Login";
SharedPreferences pref2 = context.getSharedPreferences(LOGIN2, Context.MODE_PRIVATE);
return pref2.getString("creditiApp","0");
}
我在asynctask中调用此函数,更准确地说是在" onPostExecute":
protected void onPostExecute(String result) {
String[] results = result.split("///---");
if( results[0].equals("true") ){
String LOGIN = "Login";
SharedPreferences pref = getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
String crediti22 = pref.getString("creditiAPP","no");
codiceEdit.setText(results[1]);
Bitmap bit = img.getBitmapFromURL(results[2]);
Bitmap bit2 = img.getRoundedCornerBitmap(bit,500);
imgTonda.setImageBitmap(bit2);
titolo.setText(results[3]);
autore.setText(results[4]);
int creditiNecessari = Integer.valueOf(results[5]);
int creditiDisponibiliDB = Integer.valueOf(results[6]);
int creditiDisponibiliApp = Integer.valueOf(crediti22);
if( creditiDisponibiliDB == creditiDisponibiliApp ){
crediti.setText(getString(R.string.pubbCrediti)+" "+creditiNecessari);
crediti2.setText(getString(R.string.pubbCrediti2)+" "+creditiDisponibiliApp);
}else{
String str = setVal(getApplicationContext(),String.valueOf(creditiDisponibiliDB));
Log.d("crediti",str );
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0);
TextView crediti_view = (TextView) header.findViewById(R.id.creditiMenu);
crediti_view.setText(getString(R.string.creditiDisp)+" "+creditiDisponibiliDB+" "+getString(R.string.crediti));
crediti.setText(getString(R.string.pubbCrediti)+" "+creditiNecessari);
crediti2.setText(getString(R.string.pubbCrediti2)+" "+creditiDisponibiliDB);
}
if( creditiDisponibiliDB >= creditiNecessari ){
guadagna.setVisibility(View.GONE);
download.setVisibility(View.VISIBLE);
download.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(pubblicita.this,home.class);
intent.putExtra("codice",codice);
startActivity(intent);
finish();
}
});
}else{
guadagna.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intro.setVisibility(View.GONE);
showProgress(true);
pubb.setVisibility(View.VISIBLE);
showProgress(false);
closePubb.setVisibility(View.GONE);
barTimer.setVisibility(View.VISIBLE);
timeText.setVisibility(View.VISIBLE);
barTimer.startAnimation(an);
startTimer(1);
}
});
}
intro.setVisibility(View.VISIBLE);
}else{
crediti.setVisibility(View.GONE);
crediti2.setVisibility(View.GONE);
autore.setVisibility(View.GONE);
guadagna.setVisibility(View.GONE);
titolo.setText(getString(R.string.errorePubblicita));
intro.setVisibility(View.VISIBLE);
}
showProgress(false);
mAuthTask = null;
}
同样在日志中数据会更新,但在调用其他活动时,数据不会更新:
@Override
protected void onStart(){
super.onStart();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0);
TextView nome_view = (TextView) header.findViewById(R.id.nome_view);
TextView email_view = (TextView) header.findViewById(R.id.email_view);
TextView crediti_view = (TextView) header.findViewById(R.id.creditiMenu);
String LOGIN = "Login";
SharedPreferences pref = getSharedPreferences(LOGIN, Context.MODE_PRIVATE);
String nome = pref.getString("nomeAPP","ciao");
String cognome = pref.getString("cognomeAPP","ciao");
String email = pref.getString("emailAPP","ciao");
String crediti = pref.getString("creditiAPP","ciao");
String nome_completo = nome+" "+cognome;
nome_view.setText(nome_completo);
email_view.setText(email);
crediti_view.setText(getString(R.string.creditiDisp)+" "+crediti+" "+getString(R.string.crediti));
}
你有什么建议可以给我吗?
答案 0 :(得分:2)
检查你的密钥共享偏好键是区分大小写的,我看到两个差异键
<强> creditiAPP 强> 的 creditiApp 强>
做以上改变它会起作用
答案 1 :(得分:1)
editor2.apply();
editor2.commit();
只做其中一项。不是两个,只有一个。由于您似乎是从主应用程序线程调用此代码,因此请使用apply()
。
您还有the bug that Pavan points out。您可以通过使用static final String
字段作为常量定义字符串一次来避免此问题,而不是尝试在多个位置使用相同的文字字符串。