0

2つの静的intがあるAndroidアプリを作成しています。これら両方の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();

'count' intは保存されていますが、'total'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);
}
4

3 に答える 3

1

MODE_WORLD_READABLEは使用しないでください。これは適切ではなく、非推奨です(最近からですが、とにかく使用する理由はないはずです)。代わりに、デフォルトのMODE_PRIVATEを使用してみてください。

それ以外は、コードは正常に機能しているはずです。コミットする前にtotalの値を確認し、それが整数であることを確認してください。また、エディターを2回コミットする必要はありません。1回で十分です。

于 2013-01-01T00:57:48.540 に答える
0

Eclipseを使用している場合は、DDMSパースペクティブの下のファイルエクスプローラーを使用して、コンピューター上の設定を含むファイルを転送し、それを調べます。このファイルは。の下にあり/data/data/your_package_name.your_application_name/shared_prefsます。

Pull a file from the deviceタブにフォーカスがある場合は、右上にあるを使用する必要がありFile Explorerます。

于 2013-01-01T13:06:14.250 に答える
0

私はついにこの問題を修正しました。Int 'total' は、別の int 'z' によって入力されていました。上記の inn 完全なコードを参照してください。これも合計に変更し、現在保存中です。みんなの助けに感謝します。

于 2013-01-03T14:56:23.933 に答える