save.java
public class Save extends Activity {
Intent previntent;
String string;
int levels = 2;
String blank = "Blank";
String target = "Target";
String blankval = "0";
String targetval = "0";
TextView tex1;
TextView tex2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.save);
previntent = getIntent();
string = previntent.getStringExtra("string");
tex1 = (TextView) findViewById(R.id.textView1);
tex2 = (TextView) findViewById(R.id.textView2);
save();
}
public void save()
{
loadprev();
if (string == "Blank") blankval = "1";
if (string == "Target") targetval = "1";
try {
FileOutputStream fou = openFileOutput("data.gds", MODE_PRIVATE);
try {
fou.write(blankval.getBytes());
fou.write(targetval.getBytes());
fou.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tex1.setText(blankval);
tex2.setText(targetval);
}
public void loadprev()
{
String line;
String[] temp = new String[levels];
try {
FileInputStream fin = openFileInput("data.gds");
InputStreamReader inputStreamReader = new InputStreamReader(fin);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
int tempup = 0;
try
{
while ((line = bufferedReader.readLine()) != null)
{
temp[tempup] = line;
tempup++;
}
} catch (IOException e) {e.printStackTrace();}} catch (FileNotFoundException e) {e.printStackTrace();}
if (temp[0] == "1") blankval = "1";
if (temp[0] == "0") blankval = "0";
if (temp[1] == "1") targetval = "1";
if (temp[1] == "0") targetval = "0";
}
other.java (保存前的活动)
final Intent intent = new Intent (this, Save.class);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
String string = "Blank";
intent.putExtra("string", string);
startActivity(intent);
finish();
}
}, 3000L);
other.java
中,String Blank
意味着传入
save.java
。{/ 1}} String string
save()
拨打load()
并开始阅读data.gds
,其中会显示游戏已完成级别的上一次播放(级别为blank
& target
虽然没有上传数据,所以这没什么作用)并将其价值设定为之前的游戏。save()
然后检查任何新的已完成级别。这一个是空白的并更新了要保存的值。然后将它们按级别顺序保存为零或一个,每个换行只有一个数字。data.gds (保存到文件后)
// comments not in file
1 // blank
0 // target
我得到的输出是
0-0
我会在屏幕上显示tex1
和tex2
,并在我测试时跟踪值,我想要的输出是
1-0
代码有什么问题? 我该如何解决?
另外,我不想使用SharedPreferences