Android, get pointer to ParseObject
How do you get a pointer to a parse object?
For example, my ParseUser has a column "school" which is a pointer to the class "School"
school Pointer<School>
I can get a ParseObject of type School, however I don't know how to get a pointer to this ParseObject School, so I can put it in the school's ParseUser field.
I saw similar posts for this, which said to create a parse object without data and give it the correct object id:
ParseObject mySchoolPtr = ParseObject.createWithoutData("School", mySchoolObject.getObjectId());
currentUser.put("school", mySchoolPtr);
However, after running this code, my Analyze database simply lists "(Undefined)" in the school field for the user.
Below is a code snippet that captures this entire process (after the snippet, I will explain what is happening in the snippet):
String school = schoolEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
final ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.setPassword(password);
// Query schools and search for name == school, create pointer for that one and put it for user "school"
ParseQuery<ParseObject> query = ParseQuery.getQuery("School");
query.whereEqualTo("name", school);
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject school_found, ParseException e) {
if (school_found != null) {
Log.d("School", "Retrieved " + school_found.getString("name"));
ParseObject myschoolptr = ParseObject.createWithoutData("School", school_found.getObjectId());
currentUser.put("school", myschoolptr);
} else {
Log.d("School", "Error: School name not found in Database. " + e.getMessage());
}
}
});
currentUser.saveInBackground();
showMainActivity();
In my Parse class school, I have a name with the column title. I have a user who enters their school for example "MIT". Then I ask the school class to parse the equivalent name. So this gives me the corresponding ParseObject of the school class. I want to put this in the ParseUsers learning box. However, the ParseUser field for the school is a pointer to the school's class. How can I get a pointer to my given ParseObject for input into a custom school field?
source to share
You have to call currentUser.saveInBackground();
inside GetCallback
for it to update correctly:
...
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject school_found, ParseException e) {
if (school_found != null) {
Log.d("School", "Retrieved " + school_found.getString("name"));
ParseObject myschoolptr = ParseObject.createWithoutData("School", school_found.getObjectId());
currentUser.put("school", myschoolptr);
currentUser.saveInBackground();
showMainActivity();
} else {
Log.d("School", "Error: School name not found in Database. " + e.getMessage());
}
}
});
source to share