2013-01-01 54 views
0

我正在創建一個Android應用程序,我有2個靜態整數。我想保存這兩個int的值,但目前只有1個int被保存。只有1 int使用共享偏好保存

這裏是我的拯救整數至今代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
// The activity is being created. 
setContentView(R.layout.durood); 


app_preferences = this.getSharedPreferences("myPrefscount", MODE_WORLD_READABLE); 


count = app_preferences.getInt("count", 0); 
total = app_preferences.getInt("total", 0); 

@Override 
protected void onPause() { 
super.onPause(); 

SharedPreferences.Editor editor = app_preferences.edit(); 
editor.putInt("count", count); 
editor.putInt("total", total); 
editor.commit(); 
editor.commit(); 

「計數」 INT被保存,但不是「總」廉政。關於如何糾正這個問題的任何想法?

twaddington的建議,之後的代碼

編輯最後一位:

SharedPreferences.Editor editor = app_preferences.edit(); 
editor.putInt("count", count); 
editor.putInt("total", total); 
Log.d("Test", "total: "+total); 
editor.commit(); 

我的整個代碼:

public class durood extends Activity{ 

//Count Button 
TextView txtCount; 
TextView totalCount; 
EditText enteramount; 
Button btnCount; 
Button dmute; 
Button dreset; 
Button addtotal1; 
Button addtotal2; 
Button cleartotal; 
static int count=0; 
static int total=0; 
private int x=0; 
private int y=0; 
private int z=0; 
SharedPreferences app_preferences; 
MediaPlayer mpButtonClick; 
AudioManager audioManager; 
public static boolean mutestatus=false; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
// The activity is being created. 
setContentView(R.layout.durood); 

audioManager = 
(AudioManager)getSystemService(Context.AUDIO_SERVICE); 
//SAVE COUNT 
app_preferences = this.getSharedPreferences("myPrefscount", MODE_WORLD_READABLE); 


count = app_preferences.getInt("count", 0); 
total = app_preferences.getInt("total", total++); 

txtCount = (TextView)findViewById(R.id.dcount); 
txtCount.setText("This app has been started " + count + " times."); 

txtCount = (TextView)findViewById(R.id.dcount); 
txtCount.setText("This app has been started " + count + " times."); 

enteramount = (EditText)findViewById(R.id.enteramount); 

totalCount = (TextView)findViewById(R.id.totalCount); 
totalCount.setText("This app has been started " + total + " times."); 

txtCount = (TextView)findViewById(R.id.dcount); 

//Button SOUND AND COUNT 
mpButtonClick = MediaPlayer.create(this, R.raw.bubble); 

dreset = (Button)findViewById(R.id.dreset); 

cleartotal = (Button)findViewById(R.id.cleartotal); 

txtCount = (TextView)findViewById(R.id.dcount); 
txtCount.setText(String.valueOf(count)); 

totalCount = (TextView)findViewById(R.id.totalCount); 
totalCount.setText(String.valueOf(total)); 


btnCount = (Button)findViewById(R.id.dclick); 

dmute=(Button)findViewById(R.id.dmute); 

addtotal1=(Button)findViewById(R.id.addtototal1); 
addtotal2=(Button)findViewById(R.id.addtototal2); 

btnCount.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 

count++; 
txtCount.setText(String.valueOf(count)); 
mpButtonClick.start(); 



} 
}); 

dreset.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 
count = 0; 
txtCount.setText("0"); 
SharedPreferences.Editor editor = app_preferences.edit(); 
editor.putInt("count", count); 
editor.commit(); 
} 
}); 

cleartotal.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 
total = 0; 
totalCount.setText("0"); 
SharedPreferences.Editor editor = app_preferences.edit(); 
editor.putInt("total", total); 
editor.commit(); 

} 
}); 



dmute.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 
if(!mutestatus){ 
mutestatus=true; 
audioManager.setMode(AudioManager.MODE_IN_CALL); 
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true); 

} 
else{ 
mutestatus=false; 
audioManager.setMode(AudioManager.MODE_NORMAL); 
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false); 

} 
}}); 

//add to total 1 
addtotal1.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 

x=Integer.parseInt(txtCount.getText().toString()); 
y=Integer.parseInt(totalCount.getText().toString()); 
z=x+y; 
totalCount.setText(Integer.toString(z)); 
count = 0; 
txtCount.setText("0"); 
SharedPreferences.Editor editor = app_preferences.edit(); 
editor.putInt("count", count); 
editor.commit(); 
} 


}); 

//add to total 2 
addtotal2.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 

x=Integer.parseInt(enteramount.getText().toString()); 
y=Integer.parseInt(totalCount.getText().toString()); 
z=x+y; 
totalCount.setText(Integer.toString(z)); 
enteramount.setText(""); 
} 


}); 
} 


@Override 
protected void onPause() { 
super.onPause(); 
// save count value here 

SharedPreferences.Editor editor = app_preferences.edit(); 
editor.putInt("count", count); 
editor.putInt("total", total); 
Log.d("Test", "total: "+total); 
editor.commit(); 

mutestatus=false; 
audioManager.setMode(AudioManager.MODE_NORMAL); 
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false); 
} 
+1

你不必兩次提交btw .. –

+1

什麼填充整個int?您應該在onPause中放入一條日誌語句,以確保總數包含您期望的值。 'Log.d(TAG,「Total:」+ total);' – twaddington

+0

@twaddington int的總數由count int的值和另一個編輯文本字段填充 - 當按下它們時,我有2'add to total'按鈕將該值添加到整個int中。我試着添加上面的日誌語句,但是我得到這個錯誤'TAG無法通過變量解析'。 – Mustafa

回答

0

我終於解決了這個問題。 Int'total'正在被另一個int'z'填充,見上面的完整代碼。我改變了這一點,現在它正在被保存。感謝大家的幫助。

1

不要使用MODE_WORLD_READABLE,它沒有好和棄用(因爲最近,但你不應該」無論如何,沒有任何理由這樣做),請嘗試使用默認的MODE_PRIVATE。

除此之外,你的代碼應該工作正常。驗證之前合計的值,它是一個整數。你也不需要再犯兩次你的編輯器,一次就夠了。

+0

我按照建議進行了更改,但它不起作用,與以前一樣。如何在提交之前驗證「總計」的價值? – Mustafa

0

如果您正在使用Eclipse,請使用DDMS透視圖下的文件瀏覽器將包含您的計算機上的首選項的文件轉移並檢查它。該文件應位於/data/data/your_package_name.your_application_name/shared_prefs之下。

當焦點位於File Explorer選項卡上時,您必須使用位於右上角的Pull a file from the device