Parse.com:使用parseUser,如何在我从类中解析创建的列中保存数据?

时间:2014-10-18 00:14:28

标签: java android class android-studio parse-platform

我正在为我的应用程序使用Android Studio并为我的数据库使用Parse ...并且在我创建的帐户页面上,它允许用户输入名字,姓氏,电子邮件,用户名和密码。

但我的代码是使用parseUser ...我不知道如何在数据库中设置名字和姓氏。 我知道setUsername,setPassword,setEmail是它的一部分......但是如果你在Parse中创建一个列呢?你怎么能在课堂上添加这个?

这是我的代码的一部分,它看起来像......我的问题出在我的else语句中:

          // 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) {

2 个答案:

答案 0 :(得分:13)

是的,Parse确实提供了保存用户名,密码,如 setUsername(params) setPassword(params)的方法,但是如果要向表中添加更多数据,您可以根据需要创建更多列,就像我在此代码段中所做的那样。

如果您已经在名称,电话,地址,cityState,companyId之类的解析后端创建了列,那么我就是这样做的。

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.*/

                }

            }
        });

注意:第一个参数是列名,第二个是值。所以,它基本上就像一个键值对。

这就解决了这个问题。如果这个有效,那就知道了......好运..:)

答案 1 :(得分:0)

/**
     * 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);
                    }
                }
            }
        });
    }