如果我杀死活动,textview中的文本不会保存?

时间:2015-02-11 12:45:11

标签: java android

我是一个新的蜜蜂到Android,我创建了一个小应用程序,它应该计算我参加课程的天数和我不会的天数。但是一旦我杀了应用程序并重新打开textview中的文本就会回到默认状态

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
Button b1,b2,b3;
TextView tv1,tv2,tv3;
int i=0;
int j=0;
int nd=0;
Products products;
MyDBHandler myDBHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDBHandler = new MyDBHandler(MainActivity.this,null,null,1);
    products = new Products("Present:"+i,"Absent:" + j,"Percentage" + nd +       "%");
    b1=(Button)findViewById(R.id.button);
    b2=(Button)findViewById(R.id.button2);
    b3=(Button)findViewById(R.id.calculate);
    tv1=(TextView)findViewById(R.id.textView);
    tv2=(TextView)findViewById(R.id.textView3);
    tv3=(TextView)findViewById(R.id.percentage);
    products.set_present("Present:"+i);
    products.set_absent("Absent:" + j);
    products.set_percentage("Percentage" + nd + "%");
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            i++;
            tv1.setText("Present:"+i);
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            j++;
            tv2.setText("Absent:"+j);
        }
    });
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    int dmator=i+j;
    nd=(i*100)/dmator;
    tv3.setText("Percentage:"+nd+"%");

   if(nd>65)
        Toast.makeText(MainActivity.this,"Chill you are     safe",Toast.LENGTH_LONG).show();
    if(nd<65)
        Toast.makeText(MainActivity.this,"Attention youre attendance is less     than 65",Toast.LENGTH_LONG).show();
}
});


}


}`//three buttons

//three text views

3 个答案:

答案 0 :(得分:1)

您可以使用“首选项”保存数据。检查应用程序是否第一次运行或保存一个boo,检查上次是否输入了任何数据。你喜欢哪种方式。下面是一个简短的偏好课程,我可以提出

public class PreferenceValues {

private boolean isFirstRun;


private String daysPresent;
private String daysAbsent;
private String percentage;


private Editor ed;


private static PreferenceValues mInstance = null;


private static SharedPreferences prefs;

private PreferenceValues(Context context) {
    super();
}

/**
 * Creating a singleton insatnce of the class
 * 
 * @param ctx
 * @return instance of the class
 */
public static PreferenceValues getInstance() {
    Context ctx = MyApplication.getInstance().getApplicationContext();
    if (mInstance == null) {
        mInstance = new PreferenceValues(ctx);
    }
    prefs = ctx
            .getSharedPreferences("MyPreferencess", Context.MODE_PRIVATE);

    return mInstance;
}

/**
 * To check if the application is running for the first time
 * 
 * @return
 */
public boolean isFirstRun() {
    return prefs.getBoolean("isfirstruns", true);

}

/**
 * To update the flag for the first run
 * 
 * @param isFirstRun
 */
public void setIsFirstRun(Boolean isFirstRun) {
    this.isFirstRun = isFirstRun;
    ed = prefs.edit();
    ed.putBoolean("isfirstruns", this.isFirstRun);
    ed.commit();
}


public String getDaysPresent() {
    return prefs.getString("daysPresent", "0");
}

public void setDaysPresent(String daysPresent) {
    this.daysPresent = daysPresent;
    ed = prefs.edit();
    ed.putString("daysPresent", this.daysPresent);
    ed.commit();
}

public String getdaysAbsent() {
    return prefs.getString("daysAbsent", "0");
}

public void setdaysAbsent(String daysAbsent) {
    this.daysAbsent = daysAbsent;
    ed = prefs.edit();
    ed.putString("daysAbsent", this.daysAbsent);
    ed.commit();
}

public String getpercentage() {
    return prefs.getString("percentage", "0.00");
}

public void setpercentage(String percentage) {
    this.percentage = percentage;
    ed = prefs.edit();
    ed.putString("percentage", this.percentage);
    ed.commit();

}

}

获取此示例活动代码

 public class MainActivity extends ActionBarActivity {

Button btn;
EditText t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button1);
    t1 = (EditText) findViewById(R.id.textView1);

    t1.setText(PreferenceValues.getInstance(getApplicationContext()).getDaysPresent());

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            PreferenceValues.getInstance(getApplicationContext()).setDaysPresent(t1.getText().toString());

        }
    });
}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    PreferenceValues.getInstance(getApplicationContext()).setDaysPresent(t1.getText().toString());
}

}

答案 1 :(得分:0)

如果您需要在应用关闭后存储数据,则需要使用SharedPreferences。

请参阅http://developer.android.com/training/basics/data-storage/shared-preferences.html

http://www.tutorialspoint.com/android/android_shared_preferences.htm

答案 2 :(得分:0)

之所以发生这种情况是因为活动被杀死了,所以当它再次创建时,所有对象都会被再次初始化。您可以将此数据缓存在SharedPreferences中,例如缓存在数据库中。