Add Condition for ArrayAdapter in Android - android

I'm working on Imgur API and I fetched some image links. In a gridview, i am showing all pictures but sometimes api gives ".gif" and picasso cannot load this animated pictures. I want to pass if its a gif which convertview didn't load. As you can see, I'dont want to that gray areas. Do you have any idea?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView ) {
convertView = inflater.inflate(R.layout.listview_item_image, parent, false);
}
ImageView img = (ImageView) convertView.findViewById(R.id.im);
if(datas.get(position).getLink().endsWith(".jpg") ){
Picasso.with(context)
.load(datas.get(position).getLink())
.fit()
.into(img);
}else convertView.setVisibility(View.GONE);
return convertView;
}
1: []

You must remove all objects which ends with .gif from the datas ArrayList, or you must not add them in the first place, you can give a condition while adding in ArrayList
if(data.getLink().endsWith(".jpg"))
datas.add(data)

Related

Android gridview containing NetworkImageView from volley library duplicates image on scroll

I'm using NetworkImageView for loading image from Url in my Gridview.
It works fine for the first time, but on Scroll, the grid items start duplicating at random positions.
Below is my code for GridView adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.grid_item, null);
}
NetworkImageView imageViewBand = (NetworkImageView) convertView.findViewById(R.id.imageView_grid_item);
TextView textViewBand = (TextView) convertView.findViewById(R.id.textView_grid_item);
imageViewBand.setDefaultImageResId(R.drawable.college_default_icon_grid_view);
imageViewBand.setAdjustViewBounds(true);
imageViewBand.setImageUrl(Constants.schoolImageUrl, AppController.getInstance(context).getImageLoader());
textViewBand.setText(schoolArrayList.get(position).getSchoolName());
return convertView;
}
The text in grid item works fine, but the issue occurs only with volley image loader causing image duplicate.
Please help.
Your problem is that you are reusing the view on scroll but you are not resetting the imageViewBand image url to null before loading the new image. So what you have to do is to set image url to null before reusing it
Next time try implementing a RecyclerView with a GridLayoutManager since with a RecyclerView reusing views is done automatically

Picasso doesn't load first image of ArrayAdapter

I am building a Tinder-like Android app with this library : https://github.com/Diolor/Swipecards and Picasso for image loading. Everything works fine, except the first view of the list. The image is not displayed, although the text is correct.
Views are stored in a custom subclass of ArrayAdapter. Here is the code:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
switch (getItemViewType(position)) {
case TYPE_MOVIE:
ViewHolderMovie holder;
if (convertView == null) {
holder = new ViewHolderMovie();
convertView = inflater.inflate(R.layout.movie_card, parent, false);
holder.movieCover = (ImageView)convertView.findViewById(R.id.movieCover);
holder.movieTitle = (TextView)convertView.findViewById(R.id.movieTitle);
holder.movieGenre = (TextView)convertView.findViewById(R.id.movieGenre);
convertView.setTag(holder);
} else {
holder = (ViewHolderMovie) convertView.getTag();
}
Movie movie = (Movie) data.get(position);
Picasso.with(convertView.getContext())
.load(movie.imagePath)
.resize(500, 500)
.into(holder.movieCover); // this doesn't work for the first view
holder.movieTitle.setText(movie.title);
holder.movieGenre.setText("Film au cinéma ("+movie.genre+")");
return convertView;
}
return null;
}
Do you have an idea please?
Check Your image URL path ,especially slash '/'.
Picasso don't show first image of ArrayAdapter when '/' has twice in your imageurl path.
But it show truly when you scroll Gridview or Listview

Picasso Images not loading

This is extended from the thread:Picasso Images are not loading in Gridview Android
Now the new problem is that the Picasso is not loading the images properly. If i hardcode the image url though, then the image will display (repetitively though).
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
//Inflate the XML based
Layout
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.movies_item, parent, false);
}
//Get the ImageView
ImageView movieThumbnail = (ImageView) convertView.findViewById(R.id.movies_item_thumbnail);
//Load Image into the ImageView
Picasso.with(context).load(movies.get(position).getThumbnail()).into(movieThumbnail);
//When I hardcode the url, the image shows as shown in the screenshot below
//Picasso.with(context).load("http://image.tmdb.org/t/p/w500/nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg").into(movieThumbnail);
Log.v("Populating", movies.get(position).getThumbnail());
return convertView;
}
I am completely lost. It does not throw any error too. HELP!!!!!!!!
[UPDATE]
Please refer to this link for the solution!
OkHTTP and Picasso don't run together
Well try using this
movies.get(position).getThumbnail().toString()
and make sure your URL is complete
I will give my code try this way..
Picasso.with(ctx).load(String.valueOf(images.get(position))).error(R.drawable.scorpion).into(serve_dish_iv); here variable "images" is ArrayList variable.. Try this way
Picasso.with(context).load(movies.get(position).getThumbnail()).error(R.drawable‌​.defulticon).into(movieThumbnail);
if any images are not available then default image is show.. If any issue please ask me..

ImageView of ListView loading incorrect few second when scrolling ListView

I'm using ListView with ImageView is a element of List. I have got problem when fast scrolling my List : The system seems to be recycling views until it loads the correct position's view in my ListView, resulting in duplicated images and text for a few seconds. After that, ImageView display images correctly. I'm using Universal Image Loader library to load images.
#Override
public View getView(int position, View v, ViewGroup parent) {
final PhotoViewHolder holder;
if(v == null){
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.photo_item_horizonal_layout, parent, false);
holder = new PhotoViewHolder();
holder.imgPhoto = (ImageView)v.findViewById(R.id.imgPhotoIcon);
v.setTag(holder);
}else{
holder = (PhotoViewHolder)v.getTag();
}
//item variable is a element at position of ArrayList
ImageLoader.getInstance().displayImage(item.getImageUrl(), holder.imgPhoto);
...
}
Can anyone help me?
Thanks in advanced
Do one thing Download Aquery from This link put that .jar File in your Lib folder and change your code to this:
#Override
public View getView(int position, View v, ViewGroup parent) {
final PhotoViewHolder holder;
if(v == null){
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.photo_item_horizonal_layout, parent, false);
holder = new PhotoViewHolder();
holder.imgPhoto = (ImageView)v.findViewById(R.id.imgPhotoIcon);
v.setTag(holder);
}else{
holder = (PhotoViewHolder)v.getTag();
}
//item variable is a element at position of ArrayList
AQuery aQuery=new AQuery(v)
aQuery.id(holder.imgPhoto).image(item.getImageUrl().toString());
}
Use a placeholder image while image is loading . You can do that by UIL option:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.placeholder)
......
.build();
This should solve your problem.
Are you changing the image of the ImageView by yourself in a later part of the code?
if you do, you should call
ImageLoader.getInstance().cancelDisplayTask(holder.imgPhoto);
so that the library knows that the link between the image and the imageview is broken

My ListView is not smooth when I set Bitmap to ImageView

I use adapter with ListView this is implementation of getView
#Override
public View getView(int position, View convertView, ViewGroup parent){
ItemViewHolder viewHolder;
if(convertView == null){
convertView = (RelativeLayout)inflater.inflate(resource,parent, false);
viewHolder = new ItemViewHolder();
viewHolder.itemTextName = (TextView)convertView.findViewById(R.id.item_name);
viewHolder.itemTextExpDate = (TextView)convertView.findViewById(R.id.item_exp_date);
viewHolder.itemImage = (ImageView)convertView.findViewById(R.id.item_image);
}
else{
viewHolder = (ItemViewHolder)convertView.getTag();
}
Item item = listItem.get(position);
if(listItem != null){
viewHolder.itemTextName.setText(item.getName());
viewHolder.itemTextExpDate.setText(""+item.getDaysleft());
viewHolder.itemImage.setImageBitmap(item.getImage());
}
return convertView;
}
static class ItemViewHolder {
View baseView;
TextView itemTextName;
TextView itemTextExpDate;
ImageView itemImage;
}
When I set Bitmap to ImageView the ListView is not smooth.
Am I implement the correct code in getView ?
Look on this simple code how you can use Holder pattern simple implementation you must only extern it
On start use Integer.toString or something not
viewHolder.itemTextExpDate.setText(""+item.getDaysleft());
When you use setImage... the getView will be called multiple times.
Why you are checking is it null and 1 line faster you use it ? No sense.
if(listItem != null){
viewHolder.itemTextName.setText(item.getName());
viewHolder.itemTextExpDate.setText(""+item.getDaysleft());
viewHolder.itemImage.setImageBitmap(item.getImage());
}
And the most important thing you don't set holder on the View this code doesn't crash for you?
Look at topics:
Lazy loading
getView called multiple times
getView called multiple times 2
Try adding : convertView.setTag(viewHolder) in the if-block. Also, use a method like Universal Image Loader for loading images in list view

Categories

Resources