如何在android parse.com中更新userprofile

时间:2015-08-01 06:46:22

标签: android parse-platform

/ *我已创建用户类,在parse.com android中保存用户名和密码,接下来我使用编辑按钮添加配置文件详细信息并将其与userid一起保存。现在问题是如何更新相同的用户配置文件详细信息?每次创建新用户配置文件时。帮助我GUYZ .. !!谢谢

    profileSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            countryname=country.getText().toString();
            parentname=parent.getText().toString();
            childname=child.getText().toString();
            relationname=relation.getText().toString();

            if(countryname.isEmpty() || parentname.isEmpty() ||childname.isEmpty() ||relationname.isEmpty()){
                AlertDialog.Builder builder =new AlertDialog.Builder(MyProfile.this);
                builder.setTitle("Error")
                        .setMessage("please fill all the fields")
                        .setPositiveButton(android.R.string.ok,null);
                AlertDialog dialog =builder.create();
                dialog.show();

            }else{
                pDialog =new ProgressDialog(MyProfile.this);
                pDialog.setMessage("Saving..!!");
                pDialog.setIndeterminate(true);
                pDialog.show();

                //get current user
                ParseUser parseUser =ParseUser.getCurrentUser();
                String userid =parseUser.getObjectId();

                //save data in profile class
                ParseObject mProfile =new ParseObject("Profile");

                mProfile.put("country",countryname);
                mProfile.put("parent",parentname);
                mProfile.put("child",childname);
                mProfile.put("relation",relationname);
                mProfile.put("userid", userid);
                mProfile.saveInBackground();


                Intent i =new Intent(getApplicationContext(),Home.class);
                startActivity(i);
                pDialog.dismiss();

            }

        }
    });

1 个答案:

答案 0 :(得分:0)

要首先更新用户个人资料,您需要检查个人资料是否存在于"个人资料"是否存在类,如果存在则需要更新该行的值,如果不存在则意味着您需要创建新的配置文件对象并保存它。

代码:

 // current user query

    ParseQuery<ParseUser> query= ParseUser.getQuery();
    userId= ParseUser.getCurrentUser().getObjectId();
    query.whereEqualTo("objectId", userId);

// Profile user query
    ParseQuery<ParseObject> fetchchQuery = new ParseQuery<ParseObject>("Profile");
    fetchQuery.whereMatchesQuery("userid",query);
    fetchQuery.findObjectsInBackground(new FindCallback<ParseObject>() {

            @Override
            public void done(List<ParseObject> objects, ParseException exception)  {

                if(exception == null){
                ParseObject mProfile =new ParseObject("Profile");

                 if(objects.size() > 0){
                 mProfile = objects.get(0);
                }
                mProfile.put("country",countryname);
                mProfile.put("parent",parentname);
                mProfile.put("child",childname);
                mProfile.put("relation",relationname);
                mProfile.put("userid", userid);
                mProfile.saveInBackground();

                }else{
                // error while fetching 
               }


            }
        });