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
J Smillie
Courses Plus Student 40,123 PointsStuck on another (simple looking) Android challenge!
Hi guys!
I'm stuck on this question, I know the answer is probably pretty straight forward but I can't see the wood for the trees.
In this sample to-do app, we want to save a new to-do item on Parse. Start by declaring a new ParseObject variable in the save() method and use "Task" as the class name parameter for the constructor.
Here is my (incorrect) answer:
import android.app.Activity;
import android.widget.EditText;
import android.widget.Toast;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.SaveCallback;
public class MainActivity extends Activity {
protected EditText mDescriptionField;
protected SaveCallback mSaveCallback = new SaveCallback() {
@Override
public void done(ParseException e) {
Toast.makeText(MainActivity.this, "Item saved!", Toast.LENGTH_SHORT).show();
}
};
// Some code omitted!
public void save(ParseObject Task) {
;
}
}
13 Answers
william parrish
13,774 PointsThe problem is in your declaration of the parse object, and some confusion around the save method.
``` public void save() { ParseObject task = new ParseObject("Task");
}```
Since ParseObject is a Object, to get a new one you have to create an instance of it, as i do above. The constructor for the Object allows for a String parameter, so you simply pass that in.
william parrish
13,774 PointsIn general whenever you see the term key, consider it just one part of a two piece puzzle, the "key- value" pair. The key being an identifier, often times a string, that helps us point specifically to the value we want.
In the above example i would try something like Task.setMessage("description", description);
In the above, the "description" serves as the indicated key, to help us find the true value, which is the actual String description variable.
My guess on the above is just based on the idea, that you pass the key and value as a pair, when setting them. Therefore you need both "description" and description ( which are not the same thing).
When recalling them later, you would be able to get everything in the description variable, just by passing in the hard coded string "description".
Dont feel bad, some things come easy for folks, and some things dont. I still cant get the code to show up properly formatted in my answers :-)
J Smillie
Courses Plus Student 40,123 PointsThanks William, much appreciated!
J Smillie
Courses Plus Student 40,123 PointsIf you have time could you please maybe also point me in the right direction with this one:
Now put the description string in your ParseObject using the key "description".
// Some code omitted!
public void save() { ParseObject task = new ParseObject("Task");
String description = mDescriptionField.getText().toString();
Task.setMessage("description");
}
}
I've completed the course other than this challenge now and I would really like to get it done and move on. I've been watching the video and referring to Android Studio but I just don't get this.
Anything would be really appreciated!
J Smillie
Courses Plus Student 40,123 PointsThanks mate! Managed to figure it out from that.
P.S.
If you do three backticks at the beginning and add the language (html) before you include your code, then do three backticks () at the end of your code segment. It should format correctly.
J Smillie
Courses Plus Student 40,123 PointsI really though I had it... but I don't.
Why doesn't this work please?
Finally, save this ParseObject in the background and pass in the mSaveCallback member variable.
// Some code omitted!
public void save() { ParseObject task = new ParseObject("Task");
String description = mDescriptionField.getText().toString();
task.put("description", description);
task.saveInBackground("mSaveCallBack", SaveCallBack);
}
}
william parrish
13,774 PointsClose.
What got you there, is you extended the way you would use a key-value pair, to the passing of a member variable. The key value pair relies on a string (key) and some sort of other variable (value).
The member variable sort of stands alone, and is referenced just by its name you give it upon declaration.
For the above you would then use the member variable they indicate ( however i dont see it declared in the above code, is it elsewhere? ) just like you, just without the key ( since its a member variable )
Try this. See how it goes.
task.saveInBackground(mSaveCallBack);
J Smillie
Courses Plus Student 40,123 PointsThat's actually what I thought! But apparently not... I get an error.
import android.app.Activity;
import android.widget.EditText;
import android.widget.Toast;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.SaveCallback;
public class MainActivity extends Activity {
protected EditText mDescriptionField;
protected SaveCallback mSaveCallback = new SaveCallback() {
@Override
public void done(ParseException e) {
Toast.makeText(MainActivity.this, "Item saved!", Toast.LENGTH_SHORT).show();
}
};
// Some code omitted!
public void save() { ParseObject task = new ParseObject("Task");
String description = mDescriptionField.getText().toString();
task.put("description", description);
task.saveInBackground(mSaveCallBack);
}
}
./MainActivity.java:24: cannot find symbol symbol : variable mSaveCallBack location: class MainActivity task.saveInBackground(mSaveCallBack); ^ 1 error
william parrish
13,774 PointsWhen you declared it in your own code, you did not capitalize the b in callback, the example i provided to you, has a capitalized b. make the b lowercase and it will work. ( I think )
william parrish
13,774 Pointsprotected SaveCallback mSaveCallback = new SaveCallback()
task.saveInBackground(mSaveCallBack);
just one letter is causing the problem there.
J Smillie
Courses Plus Student 40,123 PointsHa... Thanks so much! You have really helped me out - and most of it has been with pretty trivial stuff. So happy to have completed this course!
william parrish
13,774 PointsYour welcome!
For what its worth, i sort of stumbled through things myself for a while when i started with Java, and Android. What made the difference for me, was getting a solid foundation in Java itself, as a programming language, rather than trying to absorb it through the use of an entirely new to me technology platform (Android).
Once i had some practice creating classes, defining constructors, creating methods, and then creating instances of classes containing all that stuff, i was able to actually think my way through problems. The documentation on Developer.Android.com makes infinitely more sense when you are able to fully read what all is in front of you.
That made all the difference in the world really.
Anywho, good luck to you man.
J Smillie
Courses Plus Student 40,123 PointsCheers! I'm pretty happy that Treehouse has just released a Java course actually... will be moving onto that pretty swiftly. Good luck to you too!