Parse.com: with parseUser, how can I save data in a column I created in parse from the class?

二次信任 提交于 2020-01-14 08:06:09

问题


I am using Android Studio for my app and Parse for my database... and on the create an account page I have, it allows the user to enter in first name, last name, email, username, and password.

But my code is using parseUser... I don't know how to set the first and last name in the database. I know setUsername, setPassword, setEmail is a part of it... but what about if you make a column in Parse? How can you add this in your class?

This is a part of my code, what it looks like...my problem is in the else statement I have:

          // Force user to fill up the form
            if (usernametxt.equals("") && passwordtxt.equals("") && emailtxt.equals("")) {
                Toast.makeText(getApplicationContext(),
                        "Please fill in the username, password, and email fields.",
                        Toast.LENGTH_LONG).show();

            } else {
                // Save new user data into Parse.com Data Storage
                ParseUser user = new ParseUser();

                //somehow save first and last name 

                user.setEmail(emailtxt);
                user.setUsername(usernametxt);
                user.setPassword(passwordtxt);
                user.signUpInBackground(new SignUpCallback() {
                    public void done(ParseException e) {
                        if (e == null) {

回答1:


Yes, Parse does provide methods to save username, passwords like setUsername(params) and setPassword(params) but if you want to add more data to the tables, you can create more columns according to your needs as I did in this code snippet.

If you have come columns created already in parse back-end like name, phone,address,cityState,companyId, this is how I am doing it.

private void savetoParse() {

        ParseUser user = new ParseUser();
        user.setUsername(usernameEditText.getText().toString());
        user.setPassword(passEditText.getText().toString());
        user.put("name", nameEditText.getText().toString());
        user.setEmail(emailEditText.getText().toString());
        user.put("phone", phoneNoEditText.getText().toString());
        user.put("address", addressEditText.getText().toString());
        user.put("cityState", cityStateEditText.getText().toString());
        user.put("companyID", compSchoolIdEditText.getText().toString());

        user.signUpInBackground(new SignUpCallback() {

            @Override
            public void done(ParseException e) {

                if (e != null) {

                    Toast.makeText(SignupActivityUpdate.this,
                            "Saving user failed.", Toast.LENGTH_SHORT).show();
                    Log.w(TAG,
                            "Error : " + e.getMessage() + ":::" + e.getCode());

                    if (e.getCode() == 202) {

                        Toast.makeText(
                                SignupActivityUpdate.this,
                                "Username already taken. \n Please choose another username.",
                                Toast.LENGTH_LONG).show();
                        usernameEditText.setText("");
                        passEditText.setText("");
                        confirmPassEditText.setText("");

                    }

                } else {

                    Toast.makeText(SignupActivityUpdate.this, "User Saved",
                            Toast.LENGTH_SHORT).show();

                    /*Do some things here if you want to.*/

                }

            }
        });

NOTE : The first params is the column name and second is the value. So, it basically acts like a key value pair.

This is solve the problem..lemme know if this works..good luck..:)




回答2:


/**
     * user Registration here
     * save user registration value on server here
     */
    private void userRegistration()
    {
        progressDialog=new ProgressDialog(SignUpThirdPage.this);
        progressDialog.setMessage("Please Wait......");
        progressDialog.setCancelable(false);
        progressDialog.show();

        ParseUser user = new ParseUser();


        user.setUsername("Name");   
        user.setPassword(strPassword);
        user.setEmail(strEmailId);
        // surName column create on parse.com db you can check after run that code
        // like that u can add more column in signup table
         user.put("surName","Kumar");

        user.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(com.parse.ParseException e) {
                // TODO Auto-generated method stub
                if (e == null) {


                    }

                } else {
                    // Sign up didn't succeed. Look at the ParseException
                    // to figure out what went wrong
                    e.printStackTrace();
                    progressDialog.dismiss();
                    if(e.getMessage().contains("already taken")){
                        alertDialog("", "you've already sign up , lets login in ", SignUpThirdPage.this, false);    
                    }
                    else if(e.getMessage().contains("has already been taken")){
                        alertDialog("", "you've already sign up , lets login in ", SignUpThirdPage.this, false);    
                    }
                    else {
                        AppConstants.showAlertDialog("", e.getMessage(), SignUpThirdPage.this, false);
                    }
                }
            }
        });
    }


来源:https://stackoverflow.com/questions/26435079/parse-com-with-parseuser-how-can-i-save-data-in-a-column-i-created-in-parse-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!