我真的需要這個問題的幫助。這是代碼的一部分。在將輸出寫入文本文件之前,我應該解析所有文件。問題是,它的寫作或者只是解析的first xml或2nd xml文件。對於循環的每次迭代,Java Arraylist都會重新初始化
的寫入方法:
new LoadCoordinatesTask().execute();
Button btncoor = (Button) findViewById(R.id.File);
btncoor.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isSDCardWritable()) {
StringBuilder locationStrBuilder = new StringBuilder();
locationStrBuilder.append(coordinates + "\n");
String locationStr = locationStrBuilder.toString();
try {
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath()+"/FileReader");
directory.mkdirs();
File myFile = new File(directory, fileCoord);
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile, true);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(locationStr);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),"Successfully downloaded coordinates.",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
} }
else
{
// SD Card Not Available
Toast.makeText(getBaseContext(),"SD Card Not Available",Toast.LENGTH_SHORT).show();
} //else
}// onClick
});
}
的FilenameFilter方法:
public FilenameFilter xmlFilter = new FilenameFilter() {
@override
public boolean accept (File dir,String name) {
return name.endsWith(".xml") || name.endsWith (".XML")
}
};
private class LoadCoordinatesTask extends AsyncTask<String, Void, List<Coordinates>> {
for (String filename : dir.list(xmlFilter)) {
String path = dir.getAbsolutePath() + "/" + filename;
Log.i (TAG, path);
coordinates.addAll(parser.parse(path));
Log.i (TAG, "message");
}
return coordinates;
}
日誌顯示這一點:
Test /storage/sdcard/Garmin/01.xml
Test /storage/sdcard/Garmin/02.xml
Test message
Test message
但印刷僅示出了從/存儲/ SD卡/ Garmin的輸出/01.xml兩次
'files.length'返回?? –
什麼是'f'?你沒有在for循環中使用'i',所以它只執行兩次相同的代碼 –
你可能需要'files'而不是'f' – kaykay