Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Jonathan Etienne
Jonathan Etienne
24,572 Points

Issues storing new information on Parse.com

I am attempting to store a collection of information that the user would have entered into parse, to retrieve it at a later time.

In particular, user's will enter information in the following EditTexts: mName, mAge, mHeadline, mConfirm, and once the user click the confirm button, those information would be recorded at Parse, and hence would redirect the user into a new activity.

I have tried to achieve this using the following code, but my attempts so far have been unsuccessful.

I have looked into https://parse.com/docs/android_guide#users but is still confused.

If you need any clarification, kindly advise.

Thanks in advance.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_creation);

    Parse.initialize(this, "ID", "ID");

    mName = (EditText)findViewById(R.id.etxtname);
    mAge = (EditText)findViewById(R.id.etxtage);
    mHeadline = (EditText)findViewById(R.id.etxtheadline);
    mConfirm = (Button)findViewById(R.id.btnConfirm);
    mConfirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = mName.getText().toString();
            String age = mAge.getText().toString();
            String headline = mHeadline.getText().toString();

            name = name.trim();
            age = age.trim();
            headline = headline.trim();

            if (name.isEmpty() || age.isEmpty() || headline.isEmpty()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                builder.setMessage(R.string.signup_error_message)
                    .setTitle(R.string.signup_error_title)
                    .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
            else {
                // create the new user!
                ParseUser newUser = new ParseUser();
                newUser.setObjectId(name);
                newUser.setObjectId(age);;
                newUser.setObjectId(headline);
                newUser.signUpInBackground(new SignUpCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            // Success!
                            Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        }
                        else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
                            builder.setMessage(e.getMessage())
                                .setTitle(R.string.signup_error_title)
                                .setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });
            }
        }
    });

[...]

I also have two concerns:

1) Name cannot already be registered for the arrays of information (name, headline, age) to be accepted.

2) I have already created a separate Login system, where user signs in using their social media (data stored in parse). Upon the first Login of using for instance Facebook, they are prompted to this profile creation page. So my issue is relating the information entered on that page to that specific social media account on parse.

I have looked into https://parse.com/docs/relations_guide, but is still confused.

Let me know if you require any further clarification(s).

3 Answers

I believe this is your problem you need to change these Items newUser.setObjectId(name); newUser.setObjectId(age);; newUser.setObjectId(headline);

to

newUser.setUsername(name); newUser.put("age", age); newUser.put("headline", headline);

and you need to add a password field when creating a new user. newUser.setPassword("my pass");

ParseUser has three preset fields username, password and email example newUser.setUsername("username"); newUser.setPassword("there password"); newUser.setEmail("there email");

if you want to add any other fields can set them with the put method example newUser.put("field name", "value");

Jonathan Etienne
Jonathan Etienne
24,572 Points

Thank for your response Shawn, and for the clarification; I much appreciate.

These are however, my other concerns:

1) Users would signing using their social media account, where I have already added the social media integration using parse. They would then complete a profile page, where their preferred name entered would be linked to their social media information on parse. In the sense that the information entered on the profile page would be added to the same class as their signing, where for instance you would have. With that being said, there is not a need to have the user enter their password in the profile page since they would be signings using social media.

ObjectID | Username (Facebook or twitter or Google+) | Password (Facebook or twitter or Google+) | Preferred name derived from username user enters on profile page | authData (Facebook, twitter, google+) | Age | Headline (string) [...]

2) For some reason, my confirm button is not responsive. In the sense that according to the code, the followings were suppose to happen upon succesl button click:

Intent intent = new Intent(ProfileCreation.this, MoodActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent);

Thank you so much for your help thus far. If you need any clarification, let me know.

In your code your trying to create new user. What want is to add to the current user so try change this code to this

 ParseUser newUser = new ParseUser();
                newUser.setObjectId(name);
                newUser.setObjectId(age);;
                newUser.setObjectId(headline);
                 newUser.signUpInBackground(new SignUpCallback()

to

ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put(name", name);
currentUser.put("age", age);
currentUser.put("headline, headline);
currentUser.saveInBackground(saveInBackground(new SaveCallback()
Jonathan Etienne
Jonathan Etienne
24,572 Points

Hi,

Thanks for your response again. I have resolved the issue with the unresponsive button. The issue was that the following code setContentView(R.layout.activity_profile_creation) was being called twice, and hence I remove the second instance, and the issue has been resolved.

My last question is in retrieving the information recorded from another user other than the current user, where I would want to retrieve information such has "name", "age", "headline".

protected List<ParseUser> mUsers;

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
  public void done(List<ParseUser> users, ParseException e) {
    if (e == null) {
       mUsers =  users;

    } else {
        // Something went wrong.
    }
  }
});

//This give you a list of  users. Then to access one user you need to know the index the user is  at.

ParseUser user = mUsers.get(index); 

//then you can access that user's information

user.getString("name");
user.getNumber("age"); 
user.getString("headline");

you my what to look the queries section on Parse.com if you have a lot of users. You may want to filter the users, so you don't retrieve so many at once.