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

Parse.com I do not want to get the currentuser, but the login user and password

Hello everyone,

I have this app I am playing with around and when I tried to change this block of code it throws parseException error.

post.put("title", postTitle); post.put("content", postContent); post.put("author",ParseUser.getCurrentUser()); <--right here when I am changing it to: post.put("author",ParseUser.login(username,password)); it throws in error even though I have defined the variables in my code username and password and I have converted them from edittext to string. I even extend them in my current class, but no luck. I will post part of my code so you get the idea on what I am looking for.

Here is my loginClass:

import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.EditText; import android.widget.Button; import android.widget.TextView; import android.view.Window; import android.view.View; import android.content.Intent; import android.app.AlertDialog; import com.parse.ParseUser; import com.parse.LogInCallback; import com.parse.ParseException; import android.app.Activity;

public class LoginActivity extends ActionBarActivity {

protected TextView mSignUpTextView;
protected EditText mUsername;
protected EditText mPassword;
protected Button mLoginButton;

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

//TextView Button mSignUpTextView = (TextView) findViewById(R.id.signUpText); mSignUpTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {

            Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
            startActivity(intent);

        }
    });
    mUsername = (EditText) findViewById(R.id.usernameField);
    mPassword = (EditText) findViewById(R.id.passwordField);
    mLoginButton = (Button) findViewById(R.id.loginButton);
    mLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String username = mUsername.getText().toString();
            String password = mPassword.getText().toString();


            username = username.trim();
            password = password.trim();


            if (username.isEmpty() || password.isEmpty()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                builder.setMessage(R.string.loginmessage)
                        .setTitle(R.string.login_error_title)
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            } else
            // create the new user here
            {
                ParseUser.logInInBackground(username, password, new LogInCallback() {
                    @Override
                    public void done(ParseUser parseUser, ParseException e) {
                        if (e == null) {
                            //Success
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                            builder.setMessage(e.getMessage())
                                    .setTitle(R.string.login_error_title)
                                    .setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });

            }

        }
    });

}

}

Here is my editNote class in which I am trying to implement the method I mentioned above:

import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.parse.GetCallback; import com.parse.ParseObject; import com.parse.ParseQuery; import com.parse.ParseUser; import com.parse.SaveCallback;

public class EditNoteActivity extends Activity {

private Note note;
private EditText titleEditText;
private EditText contentEditText;
private String postTitle;
private String postContent;
private Button saveNoteButton;





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    //getActionBar().setDisplayHomeAsUpEnabled(true);
    setContentView(R.layout.activity_edit_note);

    Intent intent = this.getIntent();

    titleEditText = (EditText) findViewById(R.id.noteTitle);
    contentEditText = (EditText) findViewById(R.id.noteContent);

    if (intent.getExtras() != null) {
        note = new Note(intent.getStringExtra("noteId"), intent.getStringExtra("noteTitle"), intent.getStringExtra("noteContent"));

        titleEditText.setText(note.getTitle());
        contentEditText.setText(note.getContent());
    }

    saveNoteButton = (Button)findViewById(R.id.saveNote);
    saveNoteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveNote();

        }
        private void saveNote() {

            postTitle = titleEditText.getText().toString();
            postContent = contentEditText.getText().toString();

            postTitle = postTitle.trim();
            postContent = postContent.trim();

            // If user doesn't enter a title or content, do nothing
            // If user enters title, but no content, SAVE
            // If user enters content with no title, give warning
            // If user enters both title and content, SAVE

            if (!postTitle.isEmpty()) {

                // Check if post is being created or edited

                if (note == null) {
                    // create new post

                    final ParseObject post = new ParseObject("Post");

                    post.put("title", postTitle);
                    post.put("content", postContent);
                    post.put("author", ParseUser.getCurrentUser());  // this is the part where I am trying to pass the ParseUser.login(username, password)); to help view to content by any logging users

                    setProgressBarIndeterminateVisibility(true);
                    post.saveInBackground(new SaveCallback() {

                        public  void done(com.parse.ParseException e) {
                            setProgressBarIndeterminateVisibility(false);
                            if (e == null) {
                                // Saved successfully.
                                note = new Note(post.getObjectId(), postTitle, postContent);
                                Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();

                            } else {
                                // The save failed.
                                Toast.makeText(getApplicationContext(), "Failed to Save", Toast.LENGTH_SHORT).show();
                                Log.d(getClass().getSimpleName(), "User update error: " + e);
                            }
                        }
                    });

                }
                else {
                    // update post

                    ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");

                    // Retrieve the object by id
                    query.getInBackground(note.getId(), new GetCallback<ParseObject>() {
                        public void done(ParseObject post, com.parse.ParseException e) {

                            if (e == null) {
                                // Now let's update it with some new data.
                                post.put("title", postTitle);
                                post.put("content", postContent);
                                post.put("author", ParseUser.getCurrentUser());
                                setProgressBarIndeterminateVisibility(true);
                                post.saveInBackground(new SaveCallback() {
                                    public void done(com.parse.ParseException e) {
                                        setProgressBarIndeterminateVisibility(false);
                                        if (e == null) {
                                            // SAVED successfully.
                                            Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show();

                                        } else {
                                            // The SAVE failed.
                                            Toast.makeText(getApplicationContext(), "Failed to SAVE", Toast.LENGTH_SHORT).show();
                                            Log.d(getClass().getSimpleName(), "User update error: " + e);
                                        }

                                    }
                                });
                            }
                        }
                    });
                }
            }
            else if (postTitle.isEmpty() && !postContent.isEmpty()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(EditNoteActivity.this);
                builder.setMessage(R.string.edit_error_message)
                        .setTitle(R.string.edit_error_title)
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        }
    });


}

}

Hopefully that was clear.

Thanks in advance