我回到Android应用程序后尝试检索已保存的信息。我没有收到任何错误,但没有任何东西像应该的那样回到应用程序中。有关如何改进我的代码的任何建议吗?
package edu.rasmussen.mobile;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private final static String STORELOCATION="storelocation.txt";
private EditText txtEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtEditor=(EditText)findViewById(R.id.LatDegrees);
txtEditor=(EditText)findViewById(R.id.LatMinutes);
txtEditor=(EditText)findViewById(R.id.LatSeconds);
txtEditor=(EditText)findViewById(R.id.LongDegrees);
txtEditor=(EditText)findViewById(R.id.LongMinutes);
txtEditor=(EditText)findViewById(R.id.LongSeconds);
}
public void saveClicked(View v){
try {
OutputStreamWriter out=
new OutputStreamWriter(openFileOutput(STORELOCATION, 0));
out.write(txtEditor.getText().toString());
out.close();
Toast.makeText(this, "The contents are saved in the File.", Toast.LENGTH_LONG).show();
}
catch (Throwable t)
{
Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG).show();
}
}
public void readFileInEditor()
{
try
{
InputStream in = openFileInput(STORELOCATION);
if (in != null)
{
Reader tmp = null;
BufferedReader reader=new BufferedReader(tmp);
String str;
StringBuilder buf=new StringBuilder();
while ((str = reader.readLine()) !=null)
{
buf.append(str+"n");
}
in.close();
txtEditor.setText(buf.toString());
}
}
catch (java.io.FileNotFoundException e)
{
}
catch (Throwable t)
{
Toast.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
.show();
}
}
public void retrievedClicked(View v){
try {
FileInputStream fIn = openFileInput("storelocation");
InputStreamReader isr = new InputStreamReader(fIn);
Toast.makeText(this, "The contents have been retreived.", Toast.LENGTH_LONG).show();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
>
答案 0 :(得分:0)
您的EditText变量'txtEditor'只能引用一个EditText视图。通过在onCreate()中将其分配给另一个视图,它不会指向所有这些视图,而只指向最后一个视图。所以它指向LongSeconds,这是你做的最后一个任务:
txtEditor=(EditText)findViewById(R.id.LongSeconds);
假设您的保存和检索代码有效,因此您只保存并检索LongSeconds的值。要保存所有视图的状态,每个视图都需要一个不同的变量来引用它们,例如:
txtEditorLatD = (EditText)findViewById(R.id.LatDegrees);
txtEditorLatM = (EditText)findViewById(R.id.LatMinutes);
txtEditorLatS = (EditText)findViewById(R.id.LatSeconds);
txtEditorLongD = (EditText)findViewById(R.id.LongDegrees);
txtEditorLongM = (EditText)findViewById(R.id.LongMinutes);
txtEditorLongS = (EditText)findViewById(R.id.LongSeconds);
保存时,您需要依次使用某种分隔符来写出每个值的值。然后,当您重新读取数据时,请通过分隔符将其拆分并重新填充视图。
更简单的保存/恢复方法是使用SharedPreferences,这是一种常用的方法来存储活动的状态(或您希望保留的任何其他数据)。 SharedPreferences文件包含键/值对。例如,保存每个EditText的字符串值:
SharedPreferences.Editor editor = getPreferences(Activity.MODE_PRIVATE).edit();
editor.putString("LatDegrees", txtEditorLatD.getText().toString());
editor.putString("LatMinutes", txtEditorLatM.getText().toString());
//repeat for each view to be stored
editor.apply();
要检索该数据:
SharedPreferences sp = getPreferences(Activity.MODE_PRIVATE);
txtEditorLatD.setText(sp.getString("LatDegrees", ""));
txtEditorLatM.setText(sp.getString("LatMinutes", ""));
//repeat for each view to be restored