user2269164 user2269164 - 4 months ago 18
Android Question

how to use OnItemClick listener when Glide/Picasso image library is used

I have a custom listview with image and text and it is loaded properly in listview by using the Glide image library. Now i want to implement an OnItemClickListener on imageview to show the image in another activity.

Adapter code:

public class DescAdapter extends BaseAdapter {

private final listdisplay ds;
private ArrayList<descusers> dusers;
private ArrayList<descusers> orig;

public DescAdapter(listdisplay ds, ArrayList<descusers> dusers) {
this.ds = ds;
this.dusers = dusers;
}

@Override
public int getCount() {
return dusers.size();
}

@Override
public Object getItem(int position) {
return dusers.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

descusers du = dusers.get(position);
String username = du.loginname;
String descCrip=du.descCrip;
String Limage = du.image;
long Ptime = du.Ptime;

Viewholder viewholder = null;



if(convertView==null) {
viewholder = new Viewholder();
convertView = LayoutInflater.from(ds).inflate(R.layout.customlist, null);
viewholder.uname = (TextView) convertView.findViewById(R.id.username);
viewholder.desc = (TextView) convertView.findViewById(R.id.description);
viewholder.time = (TextView)convertView.findViewById(R.id.time);

viewholder.iview = (ImageView) convertView.findViewById(R.id.imageList);


convertView.setTag(viewholder);
}else {

viewholder = (Viewholder) convertView.getTag();
}

viewholder.uname.setText(username);
viewholder.desc.setText(descCrip);

if (Limage.trim().isEmpty()) {
viewholder.iview.setVisibility(View.GONE);
} else {
Glide.with(convertView.getContext()).load(Limage)
.diskCacheStrategy(DiskCacheStrategy.ALL).fitCenter().centerCrop().into(viewholder.iview);
viewholder.iview.setVisibility(View.VISIBLE);

}


return convertView;
}


OnItemClick code:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

final ImageView imageView = (ImageView) view.findViewById(R.id.imageList);
final BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
final Bitmap yourBitmap = bitmapDrawable.getBitmap();
Intent intent = new Intent(this, cust.class);
intent.putExtra("BitmapImage", yourBitmap);

}


Receiveing intent:

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
imagedisplay = (ImageView)findViewById(R.id.imagedisplay);
imagedisplay.setImageBitmap(bitmap);


But when i click on the image below exception is seen.

java.lang.ClassCastException: com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

Answer
 final BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();

Change this BitmapDrawable casting to GlideBitmapDrawable.

final GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable ) imageView.getDrawable();

I hope Picaso giving the drawable as GlideBitmapDrawable format.