繁体   English   中英

如何使用AsyncTask更新全局变量

[英]How to use AsyncTask to update a global variable

我想做什么:我正在使用自定义的“位置”适配器将行放入ListView 我试图将一个Bitmap添加到ListView的行中。 此位图来自URL。 因此,我有一个全局变量public static Bitmap bitmap并想使用AsyncTask更新此变量。 这是我的代码:

            try {
                String s = "";
                JSONArray jArray = new JSONArray(result);

                for (int i = 0; i < jArray.length(); i++) {
                    final JSONObject json = jArray.getJSONObject(i);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            try {

                                //here I am calling my new task and giving it the ID to find the image
                                BitmapWorkerTask myTask = new BitmapWorkerTask(json.getInt("ID"));
                                myTask.execute();
                                adapter.add(new Location(bitmap, json
                                        .getString("PlaceTitle"), json
                                        .getString("PlaceDetails"), json
                                        .getString("PlaceDistance"), json
                                        .getString("PlaceUpdatedTime")));

                                bitmap = null;
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    });

                }

            } catch (Exception e) {
                // TODO: handle exception
                Log.e("log_tag", "Error Parsing Data " + e.toString());
            }

这是我的AsyncTask

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private int photoID = 0;

    public BitmapWorkerTask(int photoID) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        this.photoID = photoID;
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(Integer... params) {
        String initialURL = "http://afs.spotcontent.com/img/Places/Icons/";
        final String updatedURL = initialURL + photoID + ".jpg";
        Bitmap bitmap2 = null;

        try {
            bitmap2 = BitmapFactory.decodeStream((InputStream) new URL(
                    updatedURL).getContent());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap2;
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap2) {
        bitmap = bitmap2;
    }
}

因此,对于每次迭代,我都向AsyncTask提供了一个ID,该ID用于查找图像,然后(我希望)应该更新传递给适配器的全局Bitmap。 当我运行我的应用程序时,每个列表的图片都是空的。 关于我在做什么错的任何想法吗?

考虑命令执行的以下可能顺序(请记住,任务在后台运行,因此该顺序是不确定的):

  1. myTask.execute()
  2. BitmapWorkerTask.doInBackground()
  3. adapter.Add(新位置(位图,.......
  4. BitmapWorkerTask.onPostExecute()

当您在步骤3中创建Location()对象时,传递的“位图”对象是全局对象指针。 它的值尚未生效,因为尚未调用onPostExecute()。 因此,使用非位图对象创建Location对象。 在步骤4上,当最终检索到位图时,全局对象指针的值(正确)发生了变化,但这不会影响已经在步骤2中传递给Location的(空)位图对象。这就是为什么在您的视图上看不到位图。

您可以做的是将一个附加参数传递给BitmapWorkerTask构造函数:您可以传递Location对象(或基础位图)。 然后,可以从onPostExecute()中使用检索到的位图更新该Location / bitmap对象。 您在这里不需要全局变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM