KhaleeqRana
KhaleeqRana

Reputation: 11

application crash on dialog alert click

I have created an alert dialog in fragment. It shows when user click on item view and when someone click on alert dialog option , it crashes application. Problem occurs just when perform an action to firebase but action completed successfully . I tested it to make toast and open other activity it works fine. How i can remove that one?

userDbRef.child(userID).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            final String user_name = dataSnapshot.child("user_name").getValue().toString();
                            String pic = dataSnapshot.child("user_image").getValue().toString();
                            String user_status = dataSnapshot.child("user_status").getValue().toString();

                            holder.name.setText(user_name);
                            holder.status.setText(user_status);
                            Picasso.get().load(pic).networkPolicy(NetworkPolicy.OFFLINE)
                                    .placeholder(R.drawable.default_profile_image)
                                    .into(holder.imageView);

                            holder.itemView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(final View v) {

                                    CharSequence options[] = new CharSequence[]{"Accept Request", "Cancel Request"};
                                    final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

                                    builder.setItems(options, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if (which == 0){
                                                acceptRequest(userID);

                                            }
                                            else if (which == 1){
                                                cancelRequest(userID,dialog);
                                            }
                                        }
                                    });
                                    builder.show();
                                }
                            });
                        }
                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });

private void cancelRequest(final String uid, final DialogInterface dialog) {

    friendReqDbRef.child(uid).child(currentUser_uid).removeValue()
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()){
                    }
                }

            });

}

Upvotes: -1

Views: 227

Answers (1)

Raza
Raza

Reputation: 19

A better way to do AlertDialog, just add this code in side your holder.itemView click listener.

            // Build an AlertDialog
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

            // Set a title for alert dialog
            builder.setTitle("Dialog Title");

            // Ask the final question
            builder.setMessage("Dialog Description");

            // Set click listener for alert dialog buttons
            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch(which){
                        case DialogInterface.BUTTON_POSITIVE:
                            // User clicked the Accept button
                            acceptRequest(userID);
                            break;

                        case DialogInterface.BUTTON_NEGATIVE:
                            // User clicked the Cancel button
                            cancelRequest(userID,dialog);
                            break;
                    }
                }
            };

            // Set the alert dialog yes button click listener
            builder.setPositiveButton("Accept Request", dialogClickListener);

            // Set the alert dialog no button click listener
            builder.setNegativeButton("Cancel Request",dialogClickListener);

            AlertDialog dialog = builder.create();
            // Display the alert dialog on interface
            dialog.show();

Upvotes: 1

Related Questions