0

即使我重新启动应用程序或关闭应用程序并重新启动它(持久存储),我如何保存contadorliga或转动的值?.setVisibility(View.VISIBLE);我想保存int contadorligacorrecto1.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

1

正如 rciovati 所建议的,SharedPreferences 最好使用该数据库,因为如果您想一次又一次地从数据库中检索此数据,它将导致性能问题。

还有其他选项可用于在 android 应用程序中存储持久数据。检查这个

于 2012-05-16T14:59:42.590 回答
0

您可以使用数据库来保存您想要永久存储的数据

于 2012-05-16T14:22:32.777 回答