Skip to content Skip to sidebar Skip to footer

How To Get Respective Text And Image From The Respective Recyclerview Item In Floaty Upon Clicking A Button?

I'm retrieving some data from Firebase and showing it using RecyclerView. My RecyclerView has cards in it in which the text and images are getting retrieved. The card has a button

Solution 1:

If you take a look at the Floaty code here. You will notice it uses SingletonPattern. Hence that is the reason it works for the first time but after that, it shows incorrect data. One solution that comes is to extend the Floaty class, and add your own createInstance method. The body of method would be something like this:

publicstaticsynchronized Floaty createInstance(Context context, View head, View body, int notificationId, Notification notification, FloatyOrientationListener
        floatyOrientationListener) {
    if (floaty == null) {
        floaty = newFloaty(context, head, body, notificationId, notification, floatyOrientationListener);
    }else {
        floaty.stopService();
        floaty = null;
        floaty = newFloaty(context, head, body, notificationId, notification, floatyOrientationListener);
    }
    return floaty;
}

I hope this works.

Approach 2

Another simpler, approach that comes to mind is, you initialize the Floaty only once. Then just get reference to the body in your holder and modify the ImageView and the TextView from your holder.

Solution 2:

I've tryed to rewrite your code.

  1. I don't know class of imageUIDh, but I guess it's string.

  2. imageUIDh is not defined in your code, so I don't know where do you get in from. But I've added field imageUIDh to your ViewHolder. You should update it on every bindView.

  3. some lines should be moved to ViewHolder's constructor

  4. I've added ViewHolder as a tag to btn_accept, so you can use one clickListener for all the ViewHolders.

  5. now there are much less nesting in code - it is much easier to read, debug and to find issues. may be the issue is now solved

    @OverridepublicvoidbindView(final ViewHolder holder) {
            super.bindView(holder);
    
            holder.progressBar.findViewById(R.id.progressBar_loading_image);
            holder.imageUID.setText(imageUIDh);
            holder.imageUIDh = imageUIDh;
            holder.hDescription.setText(hDescription);
    
            /// THESE 2 LINES SHOULD BE DONE IN HOLDER'S CONSTRUCTOR!!!!!
            holder.btn_accept = (Button) holder.itemView.findViewById(R.id.btn_accept);
            holder.btn_accept.setTag(holder);
    
            if (imageUIDh != null) {
                holder.progressBar.setVisibility(View.VISIBLE);
    
                if (imageUIDh.startsWith("https://firebasestorage.googleapis.com/") || imageUIDh.startsWith("content://")) {
                    doSomethingWithPicaso(imageUIDh, holder.homelessImage);
    
                    /*
                    /// THESE LINES ARE BE MOVED INTO onClick()
                    final View head = LayoutInflater.from(holder.itemView.getContext()).inflate(R.layout.widget_chathead, null);
                    final View body = LayoutInflater.from(holder.itemView.getContext()).inflate(R.layout.content_help_request_chat_head, null);
    
                    final ImageView hImageInFloaty = (ImageView) body.findViewById(R.id.hImageHF);
                    final TextView hDescriptionInFloaty = (TextView) body.findViewById(R.id.hDescriptionHF);
                    */
    
                    holder.btn_accept.setOnClickListener(newView.OnClickListener() {
                        @OverridepublicvoidonClick(View view) {
                            ViewHolderholder= (ViewHolder) view.getTag();
                            if (holder.isNetworkAvailable()) {
                                showOnNetworkAvailableAlertDialog(view.getContext(), holder.imageUIDh, holder.hDescription.getText().toString());
                            } else {
                               showNoNetworkAlert(view.getContext());
                            }
                        }
                    });
                }
            }
        }
    
        privatevoiddoSomethingWithPicaso(String imageUIDh, ImageView target, final HolderView holder){
            finalContextcontext= target.getContext();
            Picasso.with(context).cancelRequest(holder.homelessImage);
            Picasso.with(context)
                    .load(imageUIDh)
                    .error(R.drawable.ic_warning_black_24dp)
                    .into(target, newCallback() {
                        @OverridepublicvoidonSuccess() {
                            holder.progressBar.setVisibility(View.INVISIBLE);
                        }
    
                        @OverridepublicvoidonError() {
                            Toast.makeText(context, "Error occurred while loading images. Please retry.", Toast.LENGTH_SHORT).show();
                        }
                    });
        }
    
        privatevoidshowOnNetworkAvailableAlertDialog(final Context context, final String imageUIDh, final String descriptionText){
            AlertDialog.Builderbuilder=newAlertDialog.Builder(context);
            builder.setMessage(R.string.request_accepted_txt);
            builder.setPositiveButton("Yes button", newDialogInterface.OnClickListener() {
                @OverridepublicvoidonClick(DialogInterface dialogInterface, int i) {
    
                    finalViewhead= LayoutInflater.from(context).inflate(R.layout.widget_chathead, null);
                    finalViewbody= LayoutInflater.from(context).inflate(R.layout.content_help_request_chat_head, null);
    
                    finalImageViewhImageInFloaty= (ImageView) body.findViewById(R.id.hImageHF);
                    finalTextViewhDescriptionInFloaty= (TextView) body.findViewById(R.id.hDescriptionHF);
                    finalViewprogressBar= body.findViewById(R.id.progressBar_loading_image_ch);
    
                    holder.floaty = Floaty.createInstance(context, head, body, holder.NOTIFICATION_ID, notification);
                    holder.floaty.startService();
                    hDescriptionInFloaty.setText(descriptionText);
                    Picasso.with(context)
                            .load(imageUIDh)
                            .error(R.drawable.ic_warning_black_24dp)
                            .into(hImageInFloaty, newCallback() {
                                @OverridepublicvoidonSuccess() {
                                    progressBar.setVisibility(View.GONE);
                                }
    
                                @OverridepublicvoidonError() {
                                    // TODO Auto-generated method stub
    
                                }
                            });
    
                }
    
            });
            builder.setNegativeButton("Cancel", newDialogInterface.OnClickListener() {
                @OverridepublicvoidonClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(holder.itemView.getContext(), "The help-request has been rejected.", Toast.LENGTH_LONG).show();
                }
            });
            builder.show();
        }
    
        privatevoidshowNoNetworkAlert(final Context context){
            android.app.AlertDialog.Builderbuilder=newandroid.app.AlertDialog.Builder(context);
            builder.setTitle("No internet connection!");
            builder.setMessage("We need internet connection to navigate you more accurately. Please connect to the internet and then accept the help-request.");
            builder.setPositiveButton("Open Settings", newDialogInterface.OnClickListener() {
                @OverridepublicvoidonClick(DialogInterface dialogInterface, int i) {
                    Intentintent=newIntent(Settings.ACTION_SETTINGS);
                    context.startActivity(intent);
                }
            });
            builder.setNegativeButton("Cancel", null);
            builder.show();
        }
    

Post a Comment for "How To Get Respective Text And Image From The Respective Recyclerview Item In Floaty Upon Clicking A Button?"