我正在尝试将EditTextFields中的用户名和密码写入xml文件。在mainActivity中,我将获取EditTextField值并将其保存在xml文件中,而且如果该xml文件已经存储了以前存储的任何数据,那么我也希望这些数据也存在。在readActivity中,我试图从xml中读取数据。
以下是MainActivity.java的代码:
public class MainActivity extends Activity
{
EditText txtKey;
EditText txtValue;
Button btn;
Memo aLastMemo ; //
ArrayList<Memo> memos=new ArrayList<Memo>();
final static String xmlFile="userMemo09.xml";
File file ;
static int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtKey = (EditText)findViewById(R.id.txtvwKey);
txtValue = (EditText)findViewById(R.id.txtvwValue);
btn = (Button)findViewById(R.id.btnSave);
}
public void saveClk(View v)
throws SAXException, IOException
{
file = getBaseContext().getFileStreamPath(xmlFile);
if (file.exists())
{
// txtKey.setText("Exists");
getPreviousData();
String iS = String.valueOf(i++);
Log.d("insideSave", iS);
}
String key= txtKey.getText().toString();
String val= txtValue.getText().toString();
aLastMemo = new Memo(key,val);
memos.add(aLastMemo);
writeNewdata(memos);
}
private void getPreviousData()
{
XmlPullParser parser = Xml.newPullParser();
try {
InputStream fIn = getApplicationContext().openFileInput(xmlFile);
InputStreamReader isr = new InputStreamReader(fIn);
parser.setInput(isr);
int eventType = parser.getEventType();
boolean done = false;
while (eventType != XmlPullParser.END_DOCUMENT && !done){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("userName"))
{
aLastMemo = new Memo();
aLastMemo.set_key(parser.nextText()); }
if (name.equalsIgnoreCase("password"))
{ aLastMemo.set_value(parser.nextText()); }
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("password") && aLastMemo != null)
{
memos.add(aLastMemo);
String iS = String.valueOf(i++);
Log.d("insideSaveMethod", iS);
}
break;
}
eventType = parser.next();
}
fIn.close();
} catch (FileNotFoundException e) {
// TODO
} catch (IOException e) {
// TODO
} catch (Exception e){
// TODO
}
//txtv.setText(strn);
}
private void writeNewdata(ArrayList<Memo> memoArg)
{
try {
FileOutputStream fileos= getApplicationContext().openFileOutput(xmlFile, Context.MODE_PRIVATE);
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8",true);
xmlSerializer.startTag(null, "userData");
for(Memo mm: memoArg)
{ xmlSerializer.startTag(null, "userName");
xmlSerializer.text(mm.get_key());
xmlSerializer.endTag(null,"userName");
xmlSerializer.startTag(null,"password");
xmlSerializer.text(mm.get_value());
xmlSerializer.endTag(null, "password");
}
xmlSerializer.endTag(null, "userData");
xmlSerializer.endDocument();
xmlSerializer.flush();
String dataWrite=writer.toString();
fileos.write(dataWrite.getBytes());
fileos.flush();
fileos.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ReadActivity.java的代码:
public class ReadActivity extends Activity {
TextView txtv;
Spinner spin;
Button btn;
private SpinAdapter adapter;
ArrayList<Memo> memos = new ArrayList<Memo>();
Memo aLastMemo ;
final String xmlFile="userMemo09.xml";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read);
// Show the Up button in the action bar.
setupActionBar();
spin = (Spinner)findViewById(R.id.spinner1);
txtv = (TextView)findViewById(R.id.txtvwResult);
btn = (Button)findViewById(R.id.btnread);
}
public void btnreadClk(View view)
{
getInfoFromxml();
}
private void getInfoFromxml()
{
String strm="";
XmlPullParser parser = Xml.newPullParser();
try {
InputStream fIn = getApplicationContext().openFileInput(xmlFile);
InputStreamReader isr = new InputStreamReader(fIn);
parser.setInput(isr);
int eventType = parser.getEventType();
//boolean done = false;
while (eventType != XmlPullParser.END_DOCUMENT){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("userName"))
{
aLastMemo = new Memo();
aLastMemo.set_key(parser.nextText());
strm +=parser.nextText();
}
if (name.equalsIgnoreCase("password"))
{ aLastMemo.set_value(parser.nextText());
strm +=parser.nextText();
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("password") && aLastMemo != null)
{
memos.add(aLastMemo);
}
break;
}
eventType = parser.next();
}
fIn.close();
} catch (FileNotFoundException e) {
// TODO
} catch (IOException e) {
// TODO
} catch (Exception e){
// TODO
}
txtv.setText("Size: "+ String.valueOf(memos.size()) + strm);
}
}
它第一次正常工作。与我提供的条目一样多,它存储在xml中。但是当我去ReadActivity时,它应该在textView-txtv中显示arrayList大小,但它不是。再次当我回到mainActitivy而不再存储之前的值时,只需保留EditTextBoxes中的最新值。任何想法,我做错了吗?