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

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.";
}
}
});

Related

Android Parse - table of pointers to other tables, want to retrieve objects in the other tables

I feel like I'm pretty close on this one, just need the last bit.
I have the following tables:
_User (standard Parse table)
Category (object Id, name)
Exercises (object Id, name, description, thumbnail, image, etc)
and UserFavourites which is where I store the user's preferred exercises
(objectId, user->users table, exercise->exercises table, category->category table)
I have writing to Parse using pointers just fine:
//create new parse object
ParseObject favouriteExercise = new ParseObject("UserFavourites");
//create pointers to the Exercise table and Category table
ParseObject exercise = ParseObject.createWithoutData("Exercises", mExerciseId);
ParseObject category = ParseObject.createWithoutData("Category", mCategoryId);
//put those pointers into the Userfavourites table and save
favouriteExercise.put("user",ParseUser.getCurrentUser());
favouriteExercise.put("exercise",exercise);
favouriteExercise.put("category",category);
//save
favouriteExercise.saveInBackground();
Now I'm trying to retrieve all the exercises a user has favourited and put them in to a listview by searching the table for any objects that match the user's pointer to the user's table:
ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
query.whereEqualTo("user", user);
//call to parse.com to start the query
query.findInBackground(new FindCallback<Exercises>() {
#Override
public void done(List<Exercises> exercises, ParseException e) {
if (exercises != null) {
Toast.makeText(getApplicationContext(), "Favourites found, can't list yet", Toast.LENGTH_SHORT).show();
mAdapter.clear();
//add all the exercises to the list
mAdapter.addAll(exercises);
//sort the list alphabetically
mAdapter.sort(new Comparator<Exercises>() {
#Override
public int compare(Exercises exercises, Exercises t1) {
return exercises.getName().compareTo(t1.getName());
}
});
} else {
mNoFavourites.setVisibility(View.VISIBLE);
}
Where I'm stuck is when I run this I can see my query is working -> I am retrieving the 4 rows in UserFavourites that I favourited out of the table of 8, so it is filtering correctly, but the objects I'm getting aren't pointing to the exercises I want. They are just empty pointers.
Thanks.
Yes it will return only reference (Pointer). If you want actual object data call fetchInBackground
myObject.fetchInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// Success!
} else {
// Failure!
}
}
});
I figured it out based on the logic kishore jethava gave.
I queried the favorites table, then with the results I wanted (which pointed to another table) I cycled through each result and got the object it pointed to and added it to my ArrayList.
public void getFavourites() {
//set progress bar
setProgressBarIndeterminateVisibility(true);
ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
query.whereEqualTo("user", user);
query.include("exercise");
//call to parse.com to start the query
query.findInBackground(new FindCallback<Exercises>() {
#Override
public void done(List<Exercises> objects, ParseException e) {
if (objects.size() != 0) {
for(ParseObject object : objects)
{
//for each pointer found, retrieve the object it points to
obj = object.getParseObject("exercise");
mAdapter.add((Exercises) obj);
}
});
}
} else {
mNoFavourites.setVisibility(View.VISIBLE);
}
//stop progress bar
setProgressBarIndeterminateVisibility(false);
}
});
}

Add data to parse table doesnt work (android)

Hi im trying to add data to specific object in some table in parse.com this is the code :
ParseQuery<ParseObject> query = ParseQuery.getQuery("Buildings");
query.whereEqualTo("objectId", building);
query.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if(object == null) {
Log.e("object","not found");
}
else {
Log.e("object","found "+ object.getObjectId().toString());
object.put("test", "test");
object.saveInBackground();
}
}
});
i debug the code and it was fine and he really find the object and it really add a column to the table with the name test but i dont see any value inside i just see all the time (undefined)

How to update existing row in parse.com in android

I am working with parse.com.
I saved a geopoint in my table GPSLocation.
Now I want to update the existing row again and again but the problem is that my given code always create a new row.Please help me for that.
the code to save data is
ParseGeoPoint loc=new ParseGeoPoint(latitude,longitude);
ParseObject GPS = new ParseObject("GPSLocation");
GPS.put("location", loc);
GPS.saveInBackground();
code to get data and update is here
ParseQuery<ParseObject> query=new ParseQuery<ParseObject>("GPSLocation");
query.getInBackground("5dL98RycoD", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
ParseGeoPoint loca;
loca=object.getParseGeoPoint("location");
object.put("location", loca);
object.saveInBackground();
// object will be your game score
} else {
// something went wrong
}
}
});
Any help is appreciated.

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

Parse query.whereEqualTo String issue

I am using Parse backend for my android app. I need to query the database for a record that has a field with a specific string value. following is the Parse code
strObjectId is a String initialised with a Parse ObjectId as follows
String strObjectId = MyParseObject.getObjectId();
Parse code
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyDataTable");
query.whereEqualTo("code", Code);
query.getFirstInBackground(new GetCallback<ParseObject>()
{
public void done(ParseObject object, ParseException e)
{
if (object == null)
{
return;
}
else
{
ShowToast("Record found!");
}
}
});
The problem is that it works perfectly okay when 'Code' is hardcoded as follows prior to running above query
String Code = "krErZgz9Is";
But it DOES NOT work when Code is assigned Parse ObjectId as follows
String Code = strObjectId;
Obviously the Data Table does have a record with 'code' field with value 'krErZgz9Is'
Your help will be appreciated
Thanks
Have you tried to print out the strObjectId? If your object is not yet saved in Parse, or the object is not loaded from Parse. It will not have an object Id.
Also, have you tried to check the equality with the objectId directly?
query.whereEqualTo("code", MyParseObject.getObjectId());
I've got this working for me
I created an EditText and contained the text I want to find a match in my query. Just try to hide it in the layout if you don't really want to show it.
EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(strObjectId);
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyDataTable");
query.whereEqualTo("code", editText.getText().toString());
query.getFirstInBackground(new GetCallback<ParseObject>()
{
public void done(ParseObject object, ParseException e)
{
if (object == null)
{
return;
}
else
{
ShowToast("Record found!");
}
}
});
I don't get it why removing the getText() function doesn't work so you better keep it this way.

Categories

Resources