Get a child of a parent using pointer - android

I am trying to get a child when I have his parent.
I created a child (comment) , a parent (post) and a pointer from the child to the parent-
// Create the post
ParseObject myPost = new ParseObject("Post");
myPost.put("title", "I'm Hungry");
myPost.put("content", "Where should we go for lunch?");
// Create the comment
ParseObject myComment = new ParseObject("Comment");
myComment.put("content", "Let's do Sushirrito.");
// Add a relation between the Post and Comment
myComment.put("parent", myPost);
// This will save both myPost and myComment
myComment.saveInBackground();
My query:
String t="";
ParseObject c;
ParseQuery<ParseObject> query =
ParseQuery.getQuery("Comment");
query.whereEqualTo("parent",myPost );
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list,com.parse.ParseException e) {
if (e == null) {
c= list.get(0);
t= c.getString("content");
Toast.makeText(getApplicationContext(), t , Toast.LENGTH_LONG).show();
} else {
Log.d("NY", "Model.getStudentById Error: " + e.getMessage());
}
}
});
but I did not get the comment.
I tried also to put query.include("parent"); but it did not work.
what should I do??
thanks

The only thing I can figure out is that you might be trying to retrieve the myPost object from another method before it's actually been saved to Parse.
When I copied your code into a test project, I couldn't get it to work. Here's an updated version that does work.
Note: I'm chaining the operations so that I'm sure (well, as sure as I can be... you still need good error/exception handling) that the previous operation completed before performing the next.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.initialize(this, "<value>", "<value>");
createPost();
}
private void createPost() {
ParseObject myPost = new ParseObject("Post");
myPost.put("title", "I'm Hungry");
myPost.put("content", "Where should we go for lunch?");
mPost = myPost;
ParseObject myComment = new ParseObject("Comment");
myComment.put("content", "Let's do Sushirrito.");
myComment.put("parent", myPost);
myComment.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Comment");
query.whereEqualTo("parent", mPost);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, com.parse.ParseException e) {
if (e == null) {
ParseObject c = list.get(0);
String t = c.getString("content");
Toast.makeText(getApplicationContext(), t, Toast.LENGTH_LONG).show();
} else {
Log.d("NY", "Model.getStudentById Error: " + e.getMessage());
}
}
});
}
});
}
The code above shows my "Hello World" Activity and then posts a Toast that says "Let's do Sushirrito."

Related

Android Parse.com Unable to remove an item from Array field in both installation and User object

I've two array fields in my parse app one in Installation object named as "subscriber" and other in User object named as "SubsUser".
I've been trying to remove an item from both arrays by using this code...
user.getList("SubsUser").remove(item);
user.save();
installation.getList("subscriber").remove(item);
installation.save();
but even after using this code the items are visible on parse dashboard, it seems both my tables are not being updated.
You should use put method to let ParseObject aware data is dirty.
ParseUser user = ParseUser.getCurrentUser();
List<Object> list = user.getList("SubsUser");
list.remove(item);
user.put("SubsUser", list);
user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Log.d(TAG, "saved in background");
if (e == null)
//ok
else
//something else went wrong
}
});
I myself solved the problem, i was using incorrect code
the write code to remove an object or collection of object from a parse array is
List<String> list=new ArrayList<String>();
list.add(item);
user.removeAll("SubsUser",list);
user.save();
installation.removeAll("subscriber",list);
installation.save();
I have a solution too, with a dialog
final Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.dialogo_si_no);
dialog.setTitle("La Lista");
Button btnAceptar = dialog.findViewById(R.id.btnSi);
Button btnCancelar = dialog.findViewById(R.id.btnNo);
TextView lblMensaje = dialog.findViewById(R.id.lblMensaje);
lblMensaje.setText("¿Deseas eliminar este evento de tus favoritos?");
dialog.show();
btnAceptar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RelativeLayout layout = (RelativeLayout) view.getParent();
TextView lblObjectId1 = (TextView)layout.getChildAt(0);
final String objectId = lblObjectId1.getText().toString();
final ParseQuery<ParseObject> qFavoritos = ParseQuery.getQuery("favoritos").whereEqualTo("usuarioId",sharedpreferences.getString("globalUserId","0"));
try {
List<ParseObject> resultado = qFavoritos.find();
final String id = resultado.get(0).getObjectId();
qFavoritos.getInBackground(id, new GetCallback<ParseObject>() {
#Override
public void done(final ParseObject object, ParseException e) {
if (e == null) {
List<Object> lista = object.getList("eventos");
lista.remove(objectId);
object.put("eventos",lista);
object.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Log.d(TAG, "saved in background");
if (e == null) {
Log.d(TAG, "favorito eliminado");
Intent intent = new Intent(activity, EventosActivity.class);
activity.startActivity(intent);
}else{
Log.d(TAG, e.getMessage());
}
}
});
}else{
Log.d(TAG,e.getMessage());
}
}
});
}catch (Exception ex){
Log.d(TAG,ex.getMessage());
}
}
});
btnCancelar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});

Update Adapter within BaseAdapter

I am trying to update my adapter when i press "vote" button that was define inside getView() method in my EntertainerAdapter that extends BaseAdapter.
So here my example, first a snippet of my EntertainerListActivity when i set the adapter with some values retrived from Parse.com:
// some code to retrive my objects from Parse.com
for (ParseObject ent : entertainers) {
String name = ent.getString("name");
int vote = ent.getInt("vote");
aList.add(new Entertainer(name, vote));}
EntertainerAdapter adapter = new EntertainerAdapter(EntertainerListActivity.this, aList);
list.setAdapter(adapter);
and than my EntertainerAdapter, i am just reporting a simplified version of getView() to underline my problem:
vote.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Entertainer");
query.whereEqualTo("name", name.getText());
query.whereEqualTo("surname", surname.getText());
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("ZIG", "The getFirst request failed.");
} else {
object.put("vote", object.getInt("vote") + 1);
object.saveInBackground();
// OK Now i have to refresh my adapter
// I tried
notifyDataSetChanged(); // with no results
// than intent that start again my Activity
//Intent i = new Intent(mActivity, EntertainerListActivity.class);
//mActivity.startActivity(i);
// This works but i have a big issue when i press back button
}
}
});
}
});
The issue is, you are updating the data in parse and calling the notifyDataSetChanged(); But the data you passed to the adapter is still the same and that's the reason why you are not seeing any change. You should update the data in the adapter also.
vote.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Entertainer");
query.whereEqualTo("name", name.getText());
query.whereEqualTo("surname", surname.getText());
query.getFirstInBackground(new GetCallback<ParseObject>()
{
public void done(ParseObject object, ParseException e)
{
if (object == null)
{
Log.d("ZIG", "The getFirst request failed.");
}
else
{
object.put("vote", object.getInt("vote") + 1);
object.saveInBackground();
// Update the local Entertainer object here
// entertainerObject.setVote(entertainerObject.getVote() + 1);
notifyDataSetChanged();
}
}
});
}
});

use specific value from ParseObject to a new query

I'm trying to get the value relatedGuild from ParseObject relationShipObject that has been sent to another method.
My code:
private void getRelation(){
Log.i("Status:", "Retrieving current user...");
//Retrieve the current logged in user
ParseUser currentUser = ParseUser.getCurrentUser();
Log.i("Status:", "Retrieving relationship...");
//Retrieve the relationship object for currentUser
ParseQuery<ParseObject> relationQuery = ParseQuery.getQuery("relation");
query.whereEqualTo("relatedUser", currentUser);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> relationShip, ParseException e) {
if (e == null) {
for (ParseObject relationShipObject : relationShip) {
// This does not require a network access.
relationshipObject.get("relatedGuild");
getGuild(relationShipObject);
}
} else {
Log.d("relation", "Error: " + e.getMessage());
}
}
});
}
private void getGuild(ParseObject relationShipObject){
Log.d("relation", "relationShipObject:" + relationShipObject.getString("relatedGuild"));
}
When i call Log.d in method getGuild i get a value equal to null. Am I trying to retrieve the value from row relatedGuild the wrong way? If yes, do you have any solution to the problem?
Update:
When i change from getString to get("relatedGuild").toString(), i get a value that looks like this: com.parse.ParseObject#21u702b7. That means relationShipObjectcontains some kind of value i don't know how to retrieve.
Try this:
private void getRelation(){
Log.i("Status:", "Retrieving current user...");
//Retrieve the current logged in user
ParseUser currentUser = ParseUser.getCurrentUser();
Log.i("Status:", "Retrieving relationship...");
//Retrieve the relationship object for currentUser
ParseQuery<ParseObject> relationQuery = ParseQuery.getQuery("relation");
query.whereEqualTo("relatedUser", currentUser);
query.include("relatedGuild"); // <-THIS INCLUDES THE OBJECT BEHIND THE POINTER
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> relationShip, ParseException e) {
if (e == null) {
for (ParseObject relationShipObject : relationShip) {
// This does not require a network access.
relationshipObject.get("relatedGuild");
getGuild(relationShipObject);
}
} else {
Log.d("relation", "Error: " + e.getMessage());
}
}
});
}
private void getGuild(ParseObject relationShipObject){
Log.d("relation", "relationShipObject:" + relationShipObject.getString("relatedGuild"));
}

How to read data from ParseObject in Android

I'm new to Parse.com and Android and I'm struggling to read data from a Parse Object. I read the documentation and downloaded the tutorials but it is not clear. I have a ParseObject named Issue and all I need is to read data from this object and display it to the user. Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_issue);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "QKpM7ar7aWwrbEeTcrSGJ5bDnLMCUtc1kCr26Enl", "MCBdIQ6Y0dTsIoahzJ44UfR1zHZPJMQPwiETwj47");
ParseQuery<ParseObject> query = ParseQuery.getQuery("Issue");
query.whereEqualTo("objectId", "2aRwvd1Rnv");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> issueList, com.parse.ParseException e) {
if (e == null) {
Log.d("Issue", "Retrieved " + issueList.size() + " issue");
Toast.makeText(getApplicationContext(), "List" + issueList, Toast.LENGTH_LONG).show();
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
I don't understand how I can read data from this ParseObject. The returned result issueList is this: List[com.parse.ParseObject#4b079f88]
And if I use issue.getString("objectId") I get an error. Please help.
Thanks to Mr. Shubendara I was able to fix this:
if (issueList!=null){
if (issueList.size()>0){
for( int i = 0; i<issueList.size(); i++){
String o = issueList.get(i).getString("Title");
Toast.makeText(getApplicationContext(), "List" + o, Toast.LENGTH_LONG).show();
}
}

cannnot save the result of parse query in an android variabe

I am currently using Parse.com's API for an android application. I want to retreive an array from parse and save it as a list to work with later. but tha problem is when i'm out of the query my list turnes empty. can u help me?
ParseUser currentUser = ParseUser.getCurrentUser();
String userId= currentUser.getObjectId();
final ParseQuery<ParseObject> pQuery = ParseQuery.getQuery("Friendship");
pQuery.whereEqualTo("user", userId);
pQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e==null) {
if (list.size()>0) {
ParseObject p = list.get(0);
if (p.getList("friendsList")!=null) {
list11 = p.getList("friendsList");
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+ list11.get(2).toString(),
Toast.LENGTH_LONG).show();
}
}
}
}
});
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+list11.get(1).toString(),
Toast.LENGTH_LONG).show();
the first Toast is working but the second one gives me a NullPointerException.
You can only put second Toast in done method like this:-
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e==null) {
if (list.size()>0) {
ParseObject p = list.get(0);
if (p.getList("friendsList")!=null) {
list11 = p.getList("friendsList");
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+ list11.get(2).toString(),
Toast.LENGTH_LONG).show();
Toast.makeText(getActivity().getApplicationContext(),
"Posted With Success "+list11.get(1).toString(),
Toast.LENGTH_LONG).show();
}
}
}
}
Because findInBackground method start a new background thread.when you show second toast then list11 may be empty. Due to this it throws a NullPointerException

Categories

Resources