0

我创建了一个MainActivity和一个SecondActivity。当onClick MainActivity它启动时SecondActivity。完成SecondActivity后,它返回一个值,该值变成一个ImageView.setVisibility(View.VISIBLE);并生成一个counter++

我的问题是,当我关闭应用程序然后再次重新启动它时,所有值都被清除了,我的ImageView轮流ImageView.setVisibility(View.INVISIBLE);又一次counter == 0

SecondActivity即使我关闭应用程序或重新启动我的手机,我如何保存数据(结果)?我想public void onCreate(Bundle savedInstanceState) {在每次重新启动应用程序时关闭应用程序之前进行设置。


我想要的是保存“int contadorliga”、“correcto1.setVisibility(View.VISIBLE);” 和“correcto2.setVisibility(View.VISIBLE);” 所以每次我重新启动应用程序时,它都是最近一次。这是我的代码:

package com.fakur.android.futbolquiz;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class Liga extends Activity {
/** Called when the activity is first created. */

static final int LIGA = 0;
int contadorbarcelona = 0;
int contadormadrid = 0;
int contadorliga = 0;

ImageView about;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.liga);
TextView contador1 = (TextView) findViewById(R.id.contador1);
contador1.setText(contadorliga + "/20");
}

public void onBarcelonaClick(View botton) {

Intent intent = new Intent();
intent.setClass(this,Pregunta.class);
intent.putExtra("Barcelona", "Barcelona");
startActivityForResult(intent,LIGA);
}

public void onMadridClick(View botton) {

Intent intent = new Intent();
intent.setComponent(new ComponentName(this,Pregunta.class));
intent.putExtra("Madrid", "Madrid");
startActivityForResult(intent,LIGA);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

TextView contador1 = (TextView) findViewById(R.id.contador1);
ImageView correcto1 = (ImageView) findViewById(R.id.correcto1);
ImageView correcto2 = (ImageView) findViewById(R.id.correcto2);

if (requestCode == LIGA){

  if (resultCode == 1) {

      if(contadorbarcelona == 0){

          correcto1.setVisibility(View.VISIBLE);
          contadorliga++ ;
          contadorbarcelona++;
      }
  }

  if (resultCode == 2) {
      if(contadormadrid == 0){

           correcto2.setVisibility(View.VISIBLE);
           contadorliga++ ;
           contadormadrid++;
      }
  }

}

contador1.setText(contadorliga + "/20");

}
4

2 回答 2

2

如果您的数据集不是很大,就像 Gokhan 建议的那样,使用共享偏好对您来说非常有用。

同时,如果您有很多类似的数据要存储,请使用 sqlite3 数据库来存储您的数据。链接

此外,将数据存储在 onStop() 函数中。

于 2012-05-15T23:04:42.060 回答
1

共享偏好的使用最适合您,请参见此处

编辑:

  • 在下面添加了源以回应您的评论;

    private static final String PREFERENCES_CONTADORLIGA =  "ChangeThisTextWithSomethingYouPrefer";
    private static final String PREFERENCES_CONTADORLIGA_KEY = "ChangeThisTextWithSomethingYouPrefer";
    
    
    public void onCreate(Bundle savedInstanceState)
    {
    
        // Restore preferences
        SharedPreferences settings = getSharedPreferences(PREFERENCES_CONTADORLIGA, Context.MODE_PRIVATE);
        contadorliga = settings.getInt(PREFERENCES_CONTADORLIGA_KEY, 0);
    }
    
    
    
    protected void onDestroy() 
    {
    
        super.onDestroy();
    
        // We need an Editor object to make preference changes.
        // All objects are from android.context.Context
        SharedPreferences settings = getSharedPreferences(PREFERENCES_CONTADORLIGA, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(PREFERENCES_CONTADORLIGA_KEY, contadorliga);
    
        // Commit the edits!
        editor.commit();
    }
    

我希望这对你有用。祝你好运!

于 2012-05-15T22:54:42.347 回答