2015-05-02 26 views
1

我真的需要這個問題的幫助。這是代碼的一部分。在將輸出寫入文本文件之前,我應該解析所有文件。問題是,它的寫作或者只是解析的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兩次

+0

'files.length'返回?? –

+1

什麼是'f'?你沒有在for循環中使用'i',所以它只執行兩次相同的代碼 –

+1

你可能需要'files'而不是'f' – kaykay

回答

1

在下面的代碼中,您將重新分配coordinates一個新的列表對象指向,這就是爲什麼它將被重新初始化。

public List<Coordinates> coordinates = new ArrayList<Coordinates>(); 

    for (int i = 1; i <= files.length; i++) { 
    coordinates = parser.parse (f); 
    } 
    return coordinates; 

你應該嘗試:

public List<Coordinates> coordinates = new ArrayList<Coordinates>(); 

for (int i = 0; i <files.length; i++) { 
coordinates.addAll(parser.parse (f)); 
} 
return coordinates; 

而且是什麼類型的變量f,因爲我不能在代碼中找到它。我相信它應該是File類型的,但不能猜測,因爲你沒有指定。一旦你做了,我會編輯我的答案。

+0

我嘗試了** should-try **代碼,並且它打印了最後一次讀取(解析)的文件兩次。就像我已經試過的第二個代碼一樣,已經編輯了代碼來添加FilenameFilter方法 – LadyWinter

0

我想你想這樣做:

public FilenameFilter xmlFilter = new FilenameFilter() { 
     @override 
     public boolean accept (File dir,String name) { 
       return name.endsWith(".xml") || name.endsWith (".XML") 
} 
}; 

String files[] = dir.list(xmlFilter); 
File f; 
public List<Coordinates> coordinates = new ArrayList<Coordinates>(); 
for (String file_name:files) { 
    f=dir.getAbsolutePath() + "/" + filename; 
    coordinates.addAll(parser.parse (f)); 
} 
return coordinates; 

都能跟得上?

+0

謝謝先生,使用該代碼,它只打印第二個xml文件,兩次。 Y_Y – LadyWinter