有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用AsyncTask更新全局变量

我要做的是:我使用一个自定义的“位置”适配器将行放入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());
            }

这是我的任务

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查找图像,然后(我希望)应该更新传递到适配器的全局位图。当我运行我的应用程序时,每个列表的图片都是空的。你知道我做错了什么吗


共 (1) 个答案

  1. # 1 楼答案

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

    1. 我的任务。执行
    2. BitmapWorkerTask。doInBackground()
    3. 适配器。添加(新位置(位图…)
    4. BitmapWorkerTask。onPostExecute()

    在步骤3中创建Location()对象时,传递的“位图”对象是全局对象指针。由于尚未调用onPostExecute(),因此其值无效。因此,位置对象是使用非位图对象创建的。在第4步,当最终检索位图时,全局对象指针的值(正确地)被更改,但这不会影响已在第2步传递到位置的(空)位图对象。。。这就是为什么在视图中看不到位图

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