美好的一天,
我有一个问题,我会保存whith SharedPreference
简单的int值变量,
但我遇到了困难。
我有一个Flag-Variable“signum”,根据所选的ImageButton
更改值,我会保存此Flag-Value。一个Activity
使用两次方法SharedPreferences
是可能的吗?
对我来说,如果Flag-Value是String或int变量并不重要,我需要一个简单的方法。
我想(但我甚至不确定)我在代码行中的错误存储中我在代码行中:salvum2.putInt(“semper2”,1或2);插入正确的方式
为所有人提供帮助并为您提供帮助!
public class MainActivity extends Activity {
private static final String MUTATIO = "semper";
private static final String MUTATIO2 = "semper2";
private ImageButton button;
int signum ; //this is the variable that I want to save
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
choicheimagebutton();
operation();
}
private void choicheimagebutton() {
final SharedPreferences optio =
getSharedPreferences(MUTATIO, Context.MODE_PRIVATE);
final SharedPreferences optio2 =
getSharedPreferences(MUTATIO2, Context.MODE_PRIVATE);
button = (ImageButton) findViewById(R.id.imageButton1);
button.setImageResource(optio.getInt(MUTATIO, R.drawable.default));
button.setOnLongClickListener(new OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Titolo menù scelta");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Scegli l'icona");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
dialog.show();
Button dialogButtonLuce = (Button) dialog.findViewById(R.id.dialogButtonLuceID);
dialogButtonLuce.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
button.setImageResource(R.drawable.lux);
signum = 1 ;
SharedPreferences.Editor salvum = optio.edit();
salvum.putInt("semper", R.drawable.lux);
//Now I would save the flag-int-variable
salvum.putInt("semper", 1);
salvum.commit();
dialog.dismiss();
}
});
Button dialogButtonGas = (Button) dialog.findViewById(R.id.dialogButtonGasID);
dialogButtonGas.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
button.setImageResource(R.drawable.gas);
signum = 2;
SharedPreferences.Editor salvum = optio.edit();
salvum.putInt("semper", R.drawable.gas);
salvum.commit();
salvum.putInt("semper", 2);
salvum.commit();
dialog.dismiss();}});
Button RESET = (Button) dialog.findViewById(R.id.dialogRipristinaID);
RESET.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
optio.edit().clear().commit();
dialog.dismiss();
}});return true;}});}
private void operation() {
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if (signum==1) {
Toast.makeText(MainActivity.this, "I have a LUX", Toast.LENGTH_LONG)
.show();
}
if (signum==2) {
Toast.makeText(MainActivity.this, "I have GAS GAS", Toast.LENGTH_LONG)
.show();
}
else {
Toast.makeText(MainActivity.this, "VACUUM", Toast.LENGTH_LONG)
.show();
}}});}}
答案 0 :(得分:2)
SharedPreferences prefs = getSharedPreferences(MUTATIO, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("semper", R.drawable.lux );
editor.putInt("semper2", 1);
editor.commit();
答案 1 :(得分:2)
如果semper2是 STRING ,那么这是错误的(所有事件):salvum2.putString("semper2", 2);
它应该是:salvum2.putString("semper2", "2");
如果semper2是 INT ,那么这是错误的(所有事件):salvum2.putString("semper2", 2);
它应该是:salvum2.putInt("semper2", 2);
另请注意,您不需要为要保存的每个首选项键创建一个编辑器:一个就够了。
在您节省的所有费用结束时,提交一次操作。
答案 2 :(得分:0)
保存使用此
private SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
prefs.edit().putInt("userId", userId).commit();
prefsCommit();
并且可以使用此
int userId = prefs.getInt("userId", 0);