Reputation: 276
The adapter provides data for a listview. But when you scroll up and down, it shows old images and takes a couple of seconds to finish loading the images. This is true when you open the view for the first time.
public class BooksAdapter extends ArrayAdapter<Books> {
public BooksAdapter(Activity context, ArrayList<Books> word) {
super(context, 0, word);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link AndroidFlavor} object located at this position in the list
Books currentbook = getItem(position);
TextView bookView = (TextView) listItemView.findViewById(R.id.bookTittle);
String booktitle = currentbook.getBookName();
bookView.setText(booktitle);
TextView authorView = (TextView) listItemView.findViewById(R.id.authorname);
String authorname = currentbook.getAuthorName();
authorView.setText(authorname);
ImageView imageurl = (ImageView) listItemView.findViewById(R.id.imageView);
String imagelink = currentbook.getImageLink();
ImageAsyncTask task = new ImageAsyncTask(imageurl);
task.execute(imagelink);
return listItemView;
}
private class ImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
private ImageView img;
ImageAsyncTask(ImageView img) {
this.img = img;
}
@Override
protected Bitmap doInBackground(String... urls) {
URL url = null;
Bitmap bmp = null;
try {
url = new URL(urls[0]);
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error with creating URL ", e);
}
try {
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
}
return bmp;
}
@Override
protected void onPostExecute(Bitmap data) {
this.img.setImageBitmap(data);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}
Upvotes: 2
Views: 59
Reputation: 139
User Picasso best way to display image even you can set default image when image is loading see below example..
Picasso.with(context).load(imageUrl).placeholder(R.drawable.default_image).into(imageView);
compile 'com.squareup.picasso:picasso:2.4.0' just add into build.gradle
Upvotes: 1
Reputation: 166
Glide with default image if actual image not loaded from the server. Place this in app.gradle
compile 'com.github.bumptech.glide:glide:3.7.0'
Use it like,
Glide.with(this)
.load("your_image_url")
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
e.printStackTrace();
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
Log.d("d", "onResourceReady");
return false;
}
})
.error("your_default_image")// placed in drawable
.into("your_target_view");
Hope this helps.
Upvotes: 1
Reputation: 796
I will recomend to use Glide for this task and dont invent bicycle. You can just call GlideApp.with(this).load("http://url.com").into(imageView);
and Glide will do all work for you.
Upvotes: 1