How to connect or make relation between two OFFLINE Parse objects? - android

I have problem with Parse. I'm making something like Shopping List App and I can't find solution. Here is the problem:
I have clean app without connection to internet yet, but will be. I'm making offline shopping lists now so I'm creating list and pin to local database like this:
ParseObject list = new ParseObject("ShoppingList");
list.put("name", listNameString);
list.put("status", "0");
list.put("deadline",textDate.getText().toString());
list.pinInBackground();
Objects offline do not have ID.
Next I'm trying to add some products to this in next activity.
I have object of ShoppingList and new product and I'm trying to do something like this:
saveProductB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ParseObject product = new ParseObject("ProductOfList");
product.put("name", productName.getText().toString());
product.put("status", "0"); //status wykupienia produktu
product.put("amount", productAmount.getText().toString());
product.put("category", productCategory.getSelectedItem().toString());
product.put("description", productDescription.getText().toString());
product.put("measure", productMeasure.getSelectedItem().toString());
product.put("icon", productIcon.getText().toString());
product.put("belongsToOffline", list);
product.pinInBackground();
Intent intent = new Intent(AddProductToList.this,ShoppingListDetailsActivity.class);
intent.putExtra("LIST_OBJECT", list);
startActivity(intent);
}
});
but without unique ID I have no possibility to get object with this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("ProductOfList");
query.fromLocalDatastore();
query.whereEqualTo("belongsToOffline",list);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(final List<ParseObject> scoreList, ParseException e) {
if (e == null) {
for(ParseObject s : scoreList){
}
Log.d("score", "Retrieved " + scoreList.size() + " scores");
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
with list.get("name") instead of list inside belongsToOffline everything is ok but I need to let user to make various lists with the same name.
Any solution?
Thank you in advance.

Related

Retrieve ParseUsers list in Java (Android)

I have a table (ParseObject) called "Friends" with a "to" and "from" column which use pointers to objectId's of User's. This table tracks who's friends with who, how do I create a query that will return all my friends as user objects - not just the ID stored in that table?
Thanks!
The ParseUser class often confuses me as it is clearly different from a ParseObject and can't be used with the same methods (like ParseQuery.or)
Currently, I'm trying:
ParseQuery<ParseObject> friendsFrom = ParseQuery.getQuery("Friends");
friendsFrom.whereEqualTo("from", ParseUser.getCurrentUser());
ParseQuery<ParseObject> friendsTo = ParseQuery.getQuery("Friends");
friendsTo.whereEqualTo("to", ParseUser.getCurrentUser());
// where they've been accepted
friendsFrom.whereEqualTo("accepted", true);
friendsTo.whereEqualTo("accepted", true);
// combine the queries - parse does this a 'special' way
List<ParseQuery<ParseObject>> friends = new ArrayList<ParseQuery<ParseObject>>();
friends.add(friendsFrom);
friends.add(friendsTo);
ParseQuery<ParseObject> combinedQuery = ParseQuery.or(friends);
combinedQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> friends, ParseException e) {
if (friends != null) {
Log.d(getClass().getSimpleName(), "Found " + friends.size() + " friends");
contacts.clear();
for (ParseObject friend : friends) {
//show user, or whatever you'd like to do..
}
}
});
But a ParseObject cannot be cast to a ParseUser so I can't do anything here..
Update
According to Robert's advice I've tried the following, but it's still not providing me with the UserObjects I need - and for some reason, my Log aren't being written during these "findInBackground" callbacks by Parse..
ParseQuery < ParseObject > friendshipAndUserQuery = ParseQuery.getQuery("friends");
friendshipAndUserQuery.whereEqualTo("from", ParseUser.getCurrentUser().getObjectId());
friendshipAndUserQuery.include("to");
friendshipAndUserQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject friendship : objects) {
UserObject friend = (UserObject) friendship.getParseObject("to");
contacts.add(friend);
}
}
});

A different way to retrieve an Object in Parse with Android Studios

Currently I am making an Android application with Android Studios and I am using Parse to handle the data. I want to be able send (put) data such as town names and info about that town (town_names would be a column and info would be another, there for each town_names would have its own info). I can do that with ease and it uploads to the Parse database. However, the part I can not seem to figure out is how to retrieve the data points I desire. I have code to retrieve data based off of an objectId shown below however that is not exactly what I want.
ParseQuery<ParseObject> query = ParseQuery.getQuery("userMessage");
query.getInBackground("KgsLojXPcq", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
retrievedText = object.getString("info");
} else {
retrievedText = "No information.";
}
}
});
"userMessage" is the ParseObject I have created and "info" is the column I want to get text from. Instead of searching for an objectId ("KgsLojXPcq") is there a way to search for town_names? So then I could for example search the database for "New York" and "retrievedText" would be set to the info for New York.
ParseQuery
.getQuery("userMessage")
.whereEqualTo("town_names", "New York")
.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
retrievedText = object.getString("info");
} else {
retrievedText = "No information.";
}
}
});

How to query current, friends list and add it to ListView in android with parse

I've saved current user's friend list with PFRelation in android with parse and I want to query the current user's friend list to show it in listView (Or ListAdapter, I don't know which one is better)
Can you guys show me how to do it? source code provided will be great
let me know if you need any additional info!
Thank you
EDIT: I've tried this code but there's error occur says that
Error:(67, 25) error: name clash: done(List<ParseObject>,ParseException) in <anonymous com.example.krisanapongpoonsawat.chatit.User$1> and done(List<T>,ParseException) in FindCallback have the same erasure, yet neither overrides the other
where T is a type-variable:
T extends ParseObject declared in interface FindCallback
private void loadUserList()
{
ParseQuery query = new ParseQuery("Friends");
query.whereEqualTo("owner", ParseUser.getCurrentUser().getObjectId().toString());
query.findInBackground(new FindCallback() {
public void done(List<ParseObject> friendList, ParseException e) {
if (e == null) {
Log.d("score", "Retrieved " + friendList.size() + " scores");
for (int i = 0; i < friendlist.size(); i++) {
Object object = friendlist.get(i);
String name = ((ParseObject) object).getObjectId().toString();
listAdapter.add(name);
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
Does anyone know how to fix this? Thanks

How to update in parse database custom table using android

I know about how to insert in custom table in parse.com database but i don't know about it how to update. i confuse in the how to get all object value from the table. so you have any idea about it.
Thanks in Advances.
You could try to retrieve the object you want to update and then save it edited like this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.whereEqualTo("firstName", "Dan");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
ParseObject person = list.get(0);
person.put("firstName", "Johan");
person.saveInBackground();
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
For more examples you could read the chapter for updating objects in Parse.com documentation here https://www.parse.com/docs/android/guide#objects-updating-objects

Retrieve data from parse.com and populate a textfield

This is doing my head in. I got this working in iOS in about 10 mins. Clearly I'm missing something. I'm simply trying to pull data out of parse.com into a textfield. I have found lots of examples but none explaining why it's not working correctly. Below is the code pulled from parse.com site and jiggyed with. Incidentally it's wigging out on totemList.getString particularly the "getString" part.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Birds");
query.whereEqualTo("totemName", "Pigeon");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> totemList, ParseException e) {
if (e == null) {
Log.d("score", "Retrieved " + totemList.size() + " scores");
String totemDesc = totemList.getString("totemDesc");
//Get the Totems Description
TotemDescription = (TextView)findViewById(R.id.animalDesc);
TotemDescription.setText(totemDesc);
} else {
Log.d("score", "Error: " + e.getMessage());
// something went wrong
TotemDescription = (TextView)findViewById(R.id.animalDesc);
TotemDescription.setText("not bob");
}
}
});
List<> does not have a getString() method.
List<ParseObject> totemList
Perhaps what you wanted to do was to iterate over your list of ParseObject get all the descriptions:
String descriptions = null;
for (ParseObject totem : totemList) {
if (descriptions == null) {
descriptions = totem.getString("totemDesc");
} else {
descriptions = descriptions + ", " + totem.getString("totemDesc");
}
}
Something like that. Then set the resulting string as text of your text field
TotemDescription.setText(descriptions);
If you have more than one ParseObject in your List<> your text will be something like:
Pigeon Totem, Another Pigeon Totem

Categories

Resources