0

我在嘗試瞭解如何爲Asynctask中的數據對象設置值時遇到問題。從幾次讀數中,我發現我無法在AsynctaskdoInBackground()部分中設置值。就這樣說,我被困在了設置它們的地方。我知道這些值是通過執行檢查而出現的,但是當我嘗試通過執行milTuxs.urlTuxName/urlTuxPhoto來設置onPostExecute中的urlTuxName/urlTuxPhoto值時,這些變量無法識別。任何幫助將不勝感激。在AsyncTask中的GridView中爲ArrayList <object>設置值的方法/方法

private class LoadImageTask extends AsyncTask<ArrayList<Tuxedo>, Void, ArrayList<TuxLoader>> { 

    private String name; 
    Bitmap imageBitmap; 
    ArrayList<TuxLoader> milTuxs = new ArrayList<TuxLoader>(); 


    @Override 
    protected ArrayList<TuxLoader> doInBackground(ArrayList<Tuxedo>... tuxList) { 

     ArrayList<Tuxedo> t = tuxList[0]; 
     System.out.println("tuxUrlList count =" + t.size()); 
     // TODO Auto-generated method stub 
     try { 
      for(int i=0; i< t.size(); i++) { 

       Tuxedo tempList = t.get(i); 
       URL imageUrl = new URL(tempList.tuxUrlPath); 


       imageBitmap = DecodeBitmapSampleSize(imageUrl, 70, 70); 

       addBitmapToMemoryCache(String.valueOf(imageUrl), imageBitmap); 

       name = tempList.tuxName; 
       //System.out.println("Tux name is " + name); 

       TuxLoader tuxLoad = new TuxLoader(); 

           tuxLoad.urlName = name; 
           tuxLoad.bitmap = imageBitmap; 
       //tuxLoad.urlTuxName = (TextView) findViewById(R.id.grid_label); 
       //tuxLoad.urlTuxName.setText(name); 
       //tuxLoad.urlPhoto = (ImageView) findViewById(R.id.grid_image); 
       //tuxLoad.urlPhoto.setImageBitmap(imageBitmap); 

       milTuxs.add(tuxLoad); 
       //System.out.println("Mills Tuxs = " + milTuxs); 
      } 


      } catch (Exception e) { 
       Log.e("error", "Image Download Failed"); 
       //t.bitmap = null; 
       imageBitmap = null; 
     } 
      return milTuxs; 
    } 


    @Override 
    protected void onPostExecute(ArrayList<TuxLoader> milTuxs) { 
     super.onPostExecute(milTuxs); 
     // TODO Auto-generated method stub 


     System.out.println("PexMills Tuxs = " + milTuxs); 
     //Set tuxGrid as the GridView variable 
     GridView tuxGrid = (GridView) findViewById(R.id.tux_grid_view); 
     CustomAdapterGrid4 adapter = (new CustomAdapterGrid4(TuxedoActivity2.this, milTuxs)); 
     tuxGrid.setAdapter(adapter); 

    } 

回答

0

您不能在doInBackground directly中設置值。無論如何,它不起作用的原因是你在for循環中賦值,它每次都會改變它的值。所以請嘗試從上面的鏈接(just in case)的方法。另外我不得不問,將有多少值將被設置爲urlTextName/urlPhoto?

+0

實際上,這些值已正確添加到ArrayList ,它總共有40個值。原來20,但因爲有數據對象中有兩個項目,它來到40. – Skip

+0

我的意思是這部分'但是當我嘗試通過做milTuxs.urlTuxName/urlTuxPhoto設置onPostExecute urlTuxName/urlTuxPhoto值,這些變量不是recognized'。嘗試它在doInBackground與我發佈的鏈接 – user666

+0

好的將做,但不會阻止主UI? – Skip

相關問題