为什么我不能从OnPostExecute(AsyncTask)更新TextView和ImageView

时间:2017-07-31 10:16:31

标签: android android-asynctask

我有代码,我想更新InfoWindow的textview和imageview。从sqlite数据库中检索图像和文本,因此我将其放在AsyncTask中。当我想从OnPostExecute更新InfoWindow的textview和imageview时,这不起作用,infowindow保持为空。 我在StackOverflow上发现了类似的问题,但没有一个答案解决了我的问题。

这是我的代码:

   googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(Marker marker) {
                        LatLng latLng = marker.getPosition();
                        // find location id in database
                        Location location = dbhandler.getLocationByLatLng(latLng);
                        final int id = location.getId();
                        addButton.setVisibility(View.VISIBLE);
                        addButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // open load image fragment
                                android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                                android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                                LoadImageFragment fragment = new LoadImageFragment();
                                // pass id to new fragment
                                Bundle bundle = new Bundle();
                                bundle.putInt("id", id);
                                fragment.setArguments(bundle);

                                fragmentTransaction.replace(R.id.fragment_container, fragment);
                                fragmentTransaction.commit();
                            }
                        });
                        removeButton.setVisibility(View.VISIBLE);
                        removeButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // remove markers and images
                            }
                        });
                        new AsyncTask<LatLng, Void, Location>() {
                            @Override
                            protected Location doInBackground(LatLng... params) {
                                LatLng latLng = params[0];
                                Location location = dbhandler.getLocationByLatLng(latLng);
                                return location;
                            }
                            // find image and text associated with Location
                            protected void onPostExecute(Location location) {
                                new AsyncTask<Location, Void, Image>() {
                                    @Override
                                    protected Image doInBackground(Location... params) {
                                        Location location = params[0];
                                        try {
                                            image = dbhandler.getImageByLocationId(location.getId());
                                        }
                                        catch (Exception ex){
                                            Log.d("debug", "failed to fetch image");
                                            image = null;
                                        }
                                        return image;
                                    }
                                    @Override
                                    protected void onPostExecute(Image image) {
                                        // set image and description
                                        if(image != null) {
                                            infoImageView.setImageBitmap(image.getBitmap());
                                            infoTextView.setText(image.getDescription());

                                            updateInfoWindow(image);
                                        }
                                    }
                                }.execute(location);
                            }
                        }.execute(marker.getPosition());
                        marker.showInfoWindow();
                        return true;
                    }
                });

                // find Location in database

                // Setting a custom info window adapter for the google map
                googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
                    // Use default InfoWindow frame
                    @Override
                    public View getInfoWindow(Marker arg0) {
                        return null;
                    }

                    // Defines the contents of the InfoWindow
                    @Override
                    public View getInfoContents(Marker arg0) {
                        // Getting view from the layout file info_window_layout
                        View v = getActivity().getLayoutInflater().inflate(R.layout.info_window_layout, null);

                        // Getting the position from the marker
                        final LatLng latLng = arg0.getPosition();

                        infoImageView = (ImageView) v.findViewById(R.id.infoImage);
                        infoTextView = (TextView) v.findViewById(R.id.infoText);
                        if(image != null) {
                            infoImageView.setImageBitmap(image.getBitmap());
                            infoTextView.setText(image.getDescription());
                        }

                        return v;
                    }

                });


            }
        });

2 个答案:

答案 0 :(得分:0)

请在全球范围内声明您的变量

Image image;

答案 1 :(得分:0)

The Info Windows documentation包含以下注释:

  

注意:绘制的信息窗口是而不是实时视图。视图在返回时呈现为图像(使用View.draw(Canvas))。这意味着对视图的任何后续更改都不会被地图上的信息窗口反映出来。要稍后更新信息窗口(例如,在加载图像后),请致电showInfoWindow()。此外,信息窗口将不会考虑正常视图(例如触摸或手势事件)的任何典型交互。但是,您可以在整个信息窗口中收听通用点击事件,如以下部分所述。

因此,您必须保留对您要显示其信息的Marker的引用,下载图片,将图片设置为ImageView,然后重新调用marker.showInfoWindow()最后。