进度对话框未关闭,我无法更新用户个人资料

时间:2018-12-10 14:59:41

标签: android firebase-realtime-database firebase-storage

首先我从数据库中检索用户信息,然后我想更新数据,但是当我单击“保存”文本视图(我已经setOnClick)时,进度对话框显示出来,并没有消失,整个程序只是保留加载在那里

public class UserProfile extends Fragment {

    private DatabaseReference databaseReference;
    private StorageReference storageReference;

    private String link;
    private StorageTask mUploadTask;

    private Uri imageUri;
    private ProgressDialog progressDialog;
    private final static int PICK_IMAGE_REQUEST = 1;

    private CircleImageView circleImageView;

    private EditText emailText,nameText,passwordText,confirmPasswordText,mobileText,addressText;

    private TextView save;

    private List<String> userList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.user_profile, container, false);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

        emailText = (EditText) view.findViewById(R.id.input_email);
        nameText = (EditText) view.findViewById(R.id.input_name);
        passwordText = (EditText) view.findViewById(R.id.input_password);
        confirmPasswordText = (EditText) view.findViewById(R.id.input_reEnterPassword);
        mobileText = (EditText) view.findViewById(R.id.input_mobile);
        addressText = (EditText) view.findViewById(R.id.input_address);
        circleImageView = (CircleImageView) view.findViewById(R.id.circleImageView);
        save = (TextView) view.findViewById(R.id.Save);

        progressDialog = new ProgressDialog(getContext());

        emailText.setEnabled(false);

        userList = new ArrayList<>();

        String Email = getArguments().getString("Email");

        String email = Email.replace(".", ",");

        databaseReference = FirebaseDatabase.getInstance().getReference("User").child(email);

        storageReference = FirebaseStorage.getInstance().getReference("User");

        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {

                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        userList.add(String.valueOf(snapshot.getValue()));
                    }

                    emailText.setText(userList.get(2));
                    nameText.setText(userList.get(6));
                    passwordText.setText(userList.get(4));
                    confirmPasswordText.setText(userList.get(1));
                    mobileText.setText(userList.get(5));
                    addressText.setText(userList.get(0));

                    Picasso.get().load(userList.get(3)).into(circleImageView);

                    return;
                }


                if (!dataSnapshot.exists()) {
                    Toast.makeText(getActivity(), "Data not exist!!", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                return;
            }
        });

        circleImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(getContext(),
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {

                    // Permission is not granted
                    // Should we show an explanation?
                    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                            Manifest.permission.READ_EXTERNAL_STORAGE)) {
                        // Show an explanation to the user *asynchronously* -- don't block
                        // this thread waiting for the user's response! After the user
                        // sees the explanation, try again to request the permission.
                    } else {
                        // No explanation needed; request the permission
                        ActivityCompat.requestPermissions(getActivity(),
                                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                PICK_IMAGE_REQUEST);

                        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                        // app-defined int constant. The callback method gets the
                        // result of the request.
                    }
                } else {
                    selectImage();
                }

            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpdateAccount();

            }
        });

        return view;
    }


    public void selectImage() {
        Intent photoPickerIntent = new Intent();
        photoPickerIntent.setType("image/*");
        photoPickerIntent.setAction(Intent.ACTION_PICK);
        startActivityForResult(photoPickerIntent, PICK_IMAGE_REQUEST);
        }



    private String getFileExtension(Uri uri) {
        ContentResolver cR = getActivity().getContentResolver();
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        return mime.getExtensionFromMimeType(cR.getType(uri));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {


            imageUri = data.getData();
            Picasso.get().load(imageUri).into(circleImageView);

        }
    }

    public void UpdateAccount() {

        final String email = emailText.getText().toString();
        final String username = nameText.getText().toString();
        final String pass = passwordText.getText().toString();
        final String phone = mobileText.getText().toString();
        final String comfirmpass = confirmPasswordText.getText().toString();
        final String address = addressText.getText().toString();


        if ((TextUtils.isEmpty(username))) {
            nameText.setError("Username is required");
            nameText.requestFocus();
            return;
        }

        if ((TextUtils.isEmpty(pass))) {
            passwordText.setError("Password is required");
            passwordText.requestFocus();
            return;
        }

        if ((TextUtils.isEmpty(phone))) {
            mobileText.setError("Phone number is required");
            mobileText.requestFocus();
            return;
        }

        if ((TextUtils.isEmpty(comfirmpass))) {
            confirmPasswordText.setError("Confirm password is required");
            confirmPasswordText.requestFocus();
            return;
        }

        if ((TextUtils.isEmpty(address))) {
            addressText.setError("address is required");
            addressText.requestFocus();
            return;
        }

        progressDialog.setMessage("Updating your account");
        progressDialog.show();

        UpdateUser(email, username, pass, phone, comfirmpass, address);

    }

    public void UpdateUser(final String UserEmail, final String Username, final String Password,
                        final String PhoneNumber, final String confirmPassword, final String Address) {



        //first we encode the email into "," to enable check the firebase database
        final String email = UserEmail.replace(".", ",");

        databaseReference = FirebaseDatabase.getInstance().getReference("User").child(email);


        Log.d("UserEmail", databaseReference.toString());

        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {

                if (!dataSnapshot.exists()) {


                    if (imageUri != null) {
                        final StorageReference fileReference = storageReference.child(System.currentTimeMillis()
                                + "." + getFileExtension(imageUri));



                        mUploadTask = fileReference.putFile(imageUri)
                                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                    @Override
                                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                                        fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                            @Override
                                            public void onSuccess(Uri downloadUri) {

                                                link = downloadUri.toString();
                                                final User user = new User(email, Username, Password, PhoneNumber, confirmPassword,
                                                        Address, link);
                                                databaseReference.setValue(user);

                                                Toasty.success(getActivity().getApplicationContext(), "Update Successful!!", Toast.LENGTH_SHORT, true).show();
                                                progressDialog.dismiss();
                                            }
                                        });
                                    }
                                });
                    }

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                return;
            }
        });

    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
    }


}

我已经删除了一些代码,如果您需要更多信息,请在下面留下评论

0 个答案:

没有答案