2013-09-29 38 views
0

我有一個自定義圖庫,可以選擇圖像並將圖像的路徑寫入ArrayList。自定義圖庫由我的MainActivity類以onActivityResult Intent調用。問題是在我的自定義集合中,ArrayList是正確的(當我選擇4個圖像時,ArrayList中的Imagepath會得到4個字符串),但是當我將ArrayList從Galery傳回MainActivity時,我只剩下1個ImagePath ArrayList的......希望你能幫助我...通過ArrayList <String> with onActitityResult

開始在MainActivity.java

Intent in = new Intent(getApplicationContext(), AndroidCustomGalleryActivity.class); 
startActivityForResult(in, 500); 

填寫自定義庫中的ArrayList和發回的意圖,主要

final Button selectAll = (Button) findViewById(R.id.selectAll); 
    selectAll.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      final int thmbnls = thumbnailsselection.length; 
      //CheckBox cb = (CheckBox)findViewById(R.id.itemCheckBox); 
      //int cbId = cb.getId(); 

      all = true; 
      String allstring = String.valueOf(all); 
      Log.d("BOOLONCLICK", allstring); 

      for(int i =0; i<thmbnls; i++) 
      { 
       thumbnailsselection[i] = true; 

       path = new ArrayList<String>(); 
       path.add(arrPath[i]); 
       String test = path.toString(); 
       Log.d("ArrayList", test); 
       pfade = path.toArray(new String[path.size()]); 
      } 

      Intent intent = new Intent(); 
      intent.putStringArrayListExtra("values", path); 
      setResult(RESULT_OK, intent); 
      finish(); 
     } 
    }); 
活動

收到主意:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    switch(requestCode){ 
    case 500: 
     if(resultCode == RESULT_OK){ 


      pfade = (ArrayList<String>) data.getStringArrayListExtra("values"); 
      //Log.d("SINGLEPFADE", pfade[0]); 
      //filePath = (TextView)findViewById(R.id.idTxtImagePath); 
      //int bilder = pfade.size(); 
      String test = pfade.toString(); 
      //filePath.setText(bilder); 
      //String test = String.valueOf(bilder); 
      Log.d("AnzahlBilder", test); 
     } 
     break; 
    } 


} 

回答

2

因爲你重新創建循環

移動這條線內ArrayList

path = new ArrayList<String>(); 

for循環之前

+0

謝謝!現在看到它......編程像一個白癡...... – MichiK83

0

您應前的移動代碼迴路,和問題將會消失

... 
path = new ArrayList<String>(); 
for (int i = 0; i < thmbnls; i++) { 
    thumbnailsselection[i] = true; 
    path = new ArrayList<String>(); 
    path.add(arrPath[i]); 
    String test = path.toString(); 
    Log.d("ArrayList", test); 
    pfade = path.toArray(new String[path.size()]); 
} 
...