【问题标题】:Where/how to set values for ArrayList<object> in GridView in AsyncTask在哪里/如何在 AsyncTask 的 GridView 中设置 ArrayList<object> 的值
【发布时间】:2014-02-26 17:26:40
【问题描述】:

我在尝试理解如何为Asynctask 中的数据对象设置值时遇到了麻烦。从几个读数中我发现我无法在AsynctaskdoInBackground() 部分设置值。话虽如此,我很困惑在哪里设置它们。我通过检查知道这些值存在,但是当我尝试通过执行milTuxs.urlTuxName/urlTuxPhotoonPostExecute 中设置urlTuxName/urlTuxPhoto 值时,无法识别这些变量。任何帮助将不胜感激。

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);

    }

【问题讨论】:

    标签: android android-asynctask android-imageview android-gridview


    【解决方案1】:

    您不能在 doInBackground directly 中设置值。无论如何,它不起作用的原因是您在 for 循环中分配值,它每次都会更改值。所以试试上面链接中的方法(just in case)。我还要问,要为 urlTextName/urlPhoto 设置多少个值?

    【讨论】:

    • 实际上这些值已正确添加到 ArrayList 中,总共有 40 个值。本来是 20,但由于数据对象中有两项,所以变成了 40。
    • 我的意思是这部分but when I try to set the urlTuxName/urlTuxPhoto values in onPostExecute by doing milTuxs.urlTuxName/urlTuxPhoto, these variables aren't recognized。使用我发布的链接在 doInBackground 中尝试一下
    • 可以,但这不会拖住主界面吗?
    • 当然它不是完美的解决方案,还有更安全的方法,比如使用onProgressUpdate等。但是如果你不经常使用它不会造成太大的问题。另外我不知道你的 TuxLoader 类,所以我不能对此发表评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    相关资源
    最近更新 更多