只有一个数组元素传递给我的asynctask

时间:2014-02-19 14:46:26

标签: android string arraylist android-asynctask

我试图将一个简单的字符串数组传递给我的asynctask,但它只传递一个元素。我知道我的arraylist在调用asynctask之前填充了20个元素,但是当我检查doinBackground中的大小时,它只有一个。我看了一些与我相似的例子,问题和答案,但它仍然只传递了这一个元素。任何帮助将不胜感激......

new LoadImageTask().execute(tuxUrls); //tuxUrls has 20 elements
...

private class LoadImageTask extends AsyncTask<ArrayList<String>, Void, ArrayList<Bitmap>> {

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


    @Override
    //protected ArrayList<TuxLoader> doInBackground(ArrayList<String>... tuxUrls) {

        System.out.println("TuxUrlCount = " + tuxUrls.length);
        ArrayList<String> t = tuxUrls[0]; //thought I was passing entire array here

        try {
            for(int i=0; i<tuxUrls.length; i++) {

                URL imageUrl = new URL(t[i]);

                //do work


            }

              ...   

            } catch (Exception e) {
                Log.e("error", "Image Download Failed");
            }
          return milTuxs;
        }   


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

        //set the grid Adapter
                     ...

2 个答案:

答案 0 :(得分:1)

尝试这样做,

 ArrayList<String> t = new ArrayList<String>();
 t.addAll(tuxUrls); //this will add the entire list to your t provided tuxUrls is also a list.

然后将其中的每一个称为

try {
        for(int i=0; i<t.length; i++) {

            URL imageUrl = new URL(t.get(i));

            //do work


    }

答案 1 :(得分:0)

ArrayList<String> t = tuxUrls[0];

try {
    for(int i=0; i<t.size(); i++) {
        URL imageUrl = new URL(t.get(i));
        //do work
    }

          ...   

    } catch (Exception e) {
        Log.e("error", "Image Download Failed");
    }
return milTuxs;

“ArrayList ... tuxUrls”意味着您可以将任意数量的“ArrayList”传递给此函数,但由于您只传递了一个,因此您必须选择第一个元素。