androidlistviewbaseadaptergetview

How to get correct imageview from the position where button is clicked in Listview


I have a custom listview with image and text. I am loading image into Listview using the Glide library. Sometimes only text will be there without image. Now, I want to share the imageview and text using Share intent, for this purpose i have created a button. I have created a Onclick Listener for the button and it is displayed along with the each item in Listview. When i click on the button it will send the imageview to process then convert into bitmap and then it will be shared. Problem here is when i click on button the imageview of particular position is not selected instead different imageview selected. Please help.

I have OnItem listener on Imageview to display it in full screen and zooming.

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

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


        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);
            viewholder.civ = (CircleImageView)convertView.findViewById(R.id.profile_image);
            viewholder.share = (TextView)convertView.findViewById(R.id.share);

            Glide.with(convertView.getContext()).load(du.profile).placeholder(R.drawable.place).dontAnimate()
                    .centerCrop().signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
                    .into(viewholder.civ);

            convertView.setTag(viewholder);
        }else {

            viewholder = (Viewholder) convertView.getTag();

        }

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

        if (!Patterns.WEB_URL.matcher(Limage).matches()) {
           // viewholder.progress.setVisibility(View.GONE);
            viewholder.iview.setVisibility(View.GONE);
        } else {

            Glide.with(convertView.getContext()).load(Limage).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(viewholder.iview);
            viewholder.iview.setVisibility(View.VISIBLE);
        }


        final View finalConvertView = convertView;
        viewholder.iview.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View v) {

                Dialog builder = new Dialog(listdisplay, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
                 builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
                 builder.getWindow().setBackgroundDrawable(
                         new ColorDrawable(Color.BLACK));
                 builder.setContentView(R.layout.imager);

                 ImageView image = (ImageView) builder.findViewById(R.id.image);

                 Glide.with(finalConvertView.getContext()).load(du.image)
                         .diskCacheStrategy(DiskCacheStrategy.ALL).into(image);

                 builder.show();
             }
         });

        viewholder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //onShareItem(v);
                Uri bmpUri = getLocalBitmapUri(viewholder.iview);
                if (bmpUri != null) {
                    // Construct a ShareIntent with link to image
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                    shareIntent.setType("image/*");
                    // Launch sharing dialog for image
                    listdisplay.startActivity(Intent.createChooser(shareIntent, "Share Image"));

                } else {
                    // ...sharing failed, handle error
                }
             }
        });


        return convertView;
    }

How to get the correct Imageview from the position where button is clicked.


Solution

  • use this code @Override public void onClick(View v) {

        Dialog builder = new Dialog(listdisplay, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
        builder.getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.BLACK));
        builder.setContentView(R.layout.imager);
    
        ImageView image = (ImageView) builder.findViewById(R.id.image);
    
        Glide.with(finalConvertView.getContext()).load(du.image)
                .diskCacheStrategy(DiskCacheStrategy.ALL).into(image);
    
        Glide
                .with(finalConvertView.getContext())
                .load(du.image)
                .asBitmap()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                        image.setImageBitmap(resource); // Possibly runOnUiThread()
                        profileImage=resourec;
                    }
                });
    
        builder.show();
    }
    

    });