简体   繁体   中英

How to update multiple users from an administrator user in parse.com for android app

I am building an app there are a few users that are administrators. from these users I want them to be able to update others users info. below is what i came up with so far , but I am having trouble passing the users info. can some one help?

where I am having most trouble is selecting the user to be updated. the previous activity was a listview that pases the object string. I get them to correctly show on the necessary edittext but when I click the save button it does not update the user field.

Remember I do not want to update currentUser, I want to update a different user

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import java.util.List;


public class FullUserInfoActivity extends Activity {
    String objectId;
    protected EditText mName_FullUm;
    protected EditText mLname_FullUm;
    protected EditText mEmail_FullUm;
    protected TextView mHashid_FullUm;
    protected Button mPassword_FullUm;
    protected Button mUpdateUserFum;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_user_info);

        //initialize
        mName_FullUm = (EditText) findViewById(R.id.nameFum);
        mLname_FullUm = (EditText) findViewById(R.id.lnamefullUm);
        mEmail_FullUm = (EditText) findViewById(R.id.emailFum);
        mHashid_FullUm = (TextView) findViewById(R.id.hashIdFum);
        mPassword_FullUm = (Button) findViewById(R.id.forgotPasswordBtn);
        mUpdateUserFum = (Button) findViewById(R.id.updateUserFumButton);


        //get the Intent

        Intent intent = getIntent();
        objectId = intent.getStringExtra("objectId");

        final ParseQuery query = ParseUser.getQuery();
        query.getInBackground(objectId, new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject parseObject, ParseException e) {
                if (e == null) {
                    //Success we have Report


                    String hashid = parseObject.getString("username").toString();
                    String name = parseObject.getString("name").toString();
                    String lname = parseObject.getString("lastname").toString();
                    String email = parseObject.getString("email").toString();


                    mHashid_FullUm.setText(hashid);
                    mName_FullUm.setText(name);
                    mLname_FullUm.setText(lname);
                    mEmail_FullUm.setText(email);


                } else {
                    //Something Happened

                    //sorry there was a problem
                    AlertDialog.Builder builder = new AlertDialog.Builder(FullUserInfoActivity.this);
                    builder.setMessage(e.getMessage());
                    builder.setTitle(R.string.error_login_title);
                    builder.setPositiveButton(R.string.error_confirmation, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //close the dialog
                            dialog.dismiss();
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();


                }


            }
        });
        addListenerOnButton();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_full_user_info, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void addListenerOnButton() {
        mUpdateUserFum.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                ParseQuery<ParseUser> query = ParseUser.getQuery();
                query.whereEqualTo("objectId",objectId);
                query.findInBackground(new FindCallback<ParseUser>() {
                    public void done(List<ParseUser> userUpdate, ParseException e) {
                        if (e == null) {
                            // Now let's update it with some new data.
                            // will get sent to the Parse Cloud. playerName hasn't changed.
                            userUpdate.set("lastname", mLname_FullUm);
                            userUpdate.set("name", mName_FullUm);
                            userUpdate.set("email", mEmail_FullUm);
                            Toast.makeText(FullUserInfoActivity.this, R.string.report_save_confirmation, Toast.LENGTH_LONG).show();
                            userUpdate.saveInBackground();


                        } else {

                            //sorry there was a problem
                            AlertDialog.Builder builder = new AlertDialog.Builder(FullUserInfoActivity.this);
                            builder.setMessage(e.getMessage());
                            builder.setTitle(R.string.error_login_title);
                            builder.setPositiveButton(R.string.error_confirmation, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //close the dialog
                                    dialog.dismiss();
                                }
                            });
                            AlertDialog dialog = builder.create();
                            dialog.show();


                        }
                    }
                });

            }
        });

    }
}

UPDATE: I reformulated the code but I need help in the area.

public void addListenerOnButton() {
    mUpdateUserFum.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ParseQuery<ParseUser> query = ParseUser.getQuery();
            //ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
            query.whereEqualTo("objectId", objectId);
            // Retrieve the object by id
            query.findInBackground(new FindCallback<ParseUser>() {
                public void done(List<ParseUser> objects, ParseException e) {
                    if (e == null) {
                        // Now let's update it with some new data. In this case, only cheatMode and score
                        // will get sent to the Parse Cloud. playerName hasn't changed.
                        Toast.makeText(FullUserInfoActivity.this, objectId, Toast.LENGTH_LONG).show();

                    } else {

                        //sorry there was a problem
                        AlertDialog.Builder builder = new AlertDialog.Builder(FullUserInfoActivity.this);
                        builder.setMessage(e.getMessage());
                        builder.setTitle(R.string.error_login_title);
                        builder.setPositiveButton(R.string.error_confirmation, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //close the dialog
                                dialog.dismiss();
                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.show();


                    }
                }
            });

        }
    });

}

@ariefbayu: how do I apply this concept to my parseUser object. I know how to assign to a normal object, but for what I understand Users are Special objects. I know parse.com recommends

    ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("gender", "female");
query.findInBackground(new FindCallback<ParseUser>() {
  public void done(List<ParseUser> objects, ParseException e) {
    if (e == null) {
        // The query was successful.
    } else {
        // Something went wrong.
    }
  }
});

but I dont understand how to choose the a different user and assign the new values.

There a better way when dealing with one row result (or expected to only have one row data), that is by using ParseQuery.getFirstInBackground . Here is an example of how I did it:

ParseQuery<ChatMessage> query = ParseQuery.getQuery(ChatMessage.class);
query.whereEqualTo("messageId", msgId);
query.getFirstInBackground(new GetCallback<ChatMessage>() {

    @Override
    public void done(ChatMessage chatMessage, ParseException e) {
        if(e == null){
            chatMessage.setHasBeenDelivered(true);
            chatMessage.saveInBackground();
        } else{
            Log.e("AppLog", e.getMessage());
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM