0
I am trying to show an image from a URL on an "Infowindowadapter", but it is never shown. I’m using the Volley library to upload images.
Someone has an idea how to solve this problem?
//no fragment mapa
map.setInfoWindowAdapter(adapter);
//o adapter
public class InfoViewDetailAdvertiser implements InfoWindowAdapter {
private final View myContentsView;
private ArrayList<Advertiser> advertisers;
private ImageLoader mImageLoader;
private RequestQueue queue;
private NetworkImageView localImage;
private TextView localName;
private TextView localAddressStreetNumber;
private TextView localAddressDistrict;
private TextView localAddressCityState;
private TextView localPhoneNumber;
public InfoViewDetailAdvertiser(Activity activity, ArrayList<Advertiser> advertisers) {
this.advertisers = advertisers;
myContentsView = activity.getLayoutInflater().inflate(R.layout.adapter_local, null);
queue = RequestSingleton.getInstance(
activity.getApplicationContext()).getRequestQueue();
mImageLoader = new ImageLoader(this.queue, new LruBitmapCache(
LruBitmapCache.getCacheSize(activity.getApplicationContext())));
}
public Advertiser getAdvertiser(String id) {
for (Advertiser advertiser : advertisers) {
if (id.equals(String.valueOf(advertiser.getId()))) {
return advertiser;
}
}
return null;
}
@Override
public View getInfoContents(Marker marker) {
mapView();
Advertiser advertiser = getAdvertiser(marker.getTitle());
loadData(advertiser);
return myContentsView;
}
private void loadData(Advertiser advertiser) {
if (advertiser != null) {
localImage.setImageUrl(advertiser.getLogo(), mImageLoader);
localName.setText(advertiser.getName());
loadAdress(advertiser);
loadPhone(advertiser);
}
}
private void loadPhone(Advertiser advertiser) {
localPhoneNumber.setVisibility(View.VISIBLE);
if (advertiser.getPhones() != null && advertiser.getPhones().size() > 0) {
localPhoneNumber.setText(advertiser.getPhones().get(0));
} else {
localPhoneNumber.setVisibility(View.GONE);
}
}
private void loadAdress(Advertiser advertiser) {
if (advertiser.getAddress() != null) {
String street = advertiser.getAddress().getStreet();
String number = advertiser.getAddress().getNumber();
localAddressStreetNumber.setVisibility(View.VISIBLE);
if (street != null && number != null) {
localAddressStreetNumber.setText(street + ", " + number);
} else if (street != null) {
localAddressStreetNumber.setText(street);
} else {
localAddressStreetNumber.setVisibility(View.GONE);
}
localAddressDistrict.setVisibility(View.VISIBLE);
if (advertiser.getAddress().getDistrict() != null) {
localAddressDistrict.setText(advertiser.getAddress().getDistrict());
} else {
localAddressDistrict.setVisibility(View.GONE);
}
String city = advertiser.getAddress().getCity();
String state = advertiser.getAddress().getState();
localAddressCityState.setVisibility(View.VISIBLE);
if (city != null && state != null) {
localAddressCityState.setText(city + " - " + state);
} else if (city != null) {
localAddressCityState.setText(city);
} else if (state != null) {
localAddressCityState.setText(state);
} else {
localAddressCityState.setVisibility(View.GONE);
}
}
}
private void mapView() {
localImage = ((NetworkImageView) myContentsView.findViewById(R.id.item_imagem_local));
localName = ((TextView) myContentsView.findViewById(R.id.item_nome_local));
localAddressStreetNumber = ((TextView) myContentsView.findViewById(R.id.item_endereco_logradouro_numero));
localAddressDistrict = ((TextView) myContentsView.findViewById(R.id.item_endereco_bairro));
localAddressCityState = ((TextView) myContentsView.findViewById(R.id.item_endereco_cidade_estado));
localPhoneNumber = ((TextView) myContentsView.findViewById(R.id.item_telefone_local));
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
}
//classe LruBitmapCache
public class LruBitmapCache extends LruCache<String, Bitmap>
implements ImageCache {
public LruBitmapCache(int maxSize) {
super(maxSize);
}
public LruBitmapCache(Context ctx) {
this(getCacheSize(ctx));
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
// Returns a cache size equal to approximately three screens worth of images.
public static int getCacheSize(Context ctx) {
final DisplayMetrics displayMetrics = ctx.getResources().
getDisplayMetrics();
final int screenWidth = displayMetrics.widthPixels;
final int screenHeight = displayMetrics.heightPixels;
// 4 bytes per pixel
final int screenBytes = screenWidth * screenHeight * 4;
return screenBytes * 3;
}
}
//classe RequestSingleton
public class RequestSingleton{
private static RequestSingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private RequestSingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized RequestSingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new RequestSingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
Can you put how you’re looking for the image with Volley? It’s using an Imageloader or Imagerequest?
– Wakim
I see no problem in your code, but it was not clear how from the
InfoWindowAdapter
you are making the request to Volley. Could add this code snippet?– Wakim
The
advertiser
is filled in correctly? In thesetImageUrl
is making a mistake?– Wakim
No error occurs. I have checked that the data is correctly filled, and are.
– franM
It’s as if Adapter didn’t update after downloading the image
– franM
Try to use the
ImageRequest
, placing a breakpoint on callback Just to make sure it’s being loaded. You have an example here: http://developer.android.com/training/volley/request.html#request-image.– Wakim
The Adapter doesn’t even get a response from the requisition. The same code I’m using on this Adapter, use on a Basedapter and the loading goes smoothly.
– franM
Thanks for the help. I managed to solve the problem. I downloaded it by hand, without the library.
– franM