st_dec st_dec - 5 months ago 9
Android Question

How to hide ImageView in ListView correctly?

I want to add to ListView information about widgets:

public class Widget {
String w_type;
String title;
String desc;
String img;
//....
}


I want to show image in listview if img field isn't empty and hide ImageView in ListView if it's empty. There is current code of getView method from my adapter for listview:

public View getView(int i, View someView, ViewGroup arg2) {
LayoutInflater inflater = LayoutInflater.from(context);
if (someView == null) {
someView = inflater.inflate(R.layout.widgets_list, arg2, false);
}

ImageView img = (ImageView) someView.findViewById(R.id.pic);

if (!img_url.equals("")){
Picasso.with(context).load(img_url).into(img);
}
else {
img.setVisibility(View.GONE);
}

return someView;
}


It hiding ImageView not only when img field is empty but sometimes when it's not empty too. How can I fix it?

Answer

I think your ImageView is still hidden from an earlier call to img.setVisibility(View.GONE); so just re-show it again.

   if (!img_url.equals("")){
       Picasso.with(context).load(img_url).into(img);
           img.setVisibility(View.VISIBLE);
       }
       else {
           img.setVisibility(View.GONE);
       }
Comments