Parse: how to get pinned object without id - android

Ive got application on android which should work without internet and with parse database when internet is on.
Also I faced with problem of getting of pinned ParseObject which not saved in online database before.
So what I do:
ParseObject car = new ParseObject("cat");
cat.put("name","Pussy");
cat.pinInBackground();
So, now I want to get this cat by query with query.getInBackground but, I cant do it because I haven't objectId, which automatically generated only after saving in online database.

You can search for cats (objects) with given properties in the local datastore:
ParseQuery<ParseObject> query = ParseQuery.getQuery("cat");
query.fromLocalDatastore();
query.whereEqualTo("name", "Pussy");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> catList, ParseException e) {
if (e == null) {
Log.d("cat", "Retrieved " + catList.size() + " cats");
} else {
Log.d("cat", "Error: " + e.getMessage());
}
}
});
However, if name is the only property, you'll likely get a list of objects with more than one entry. Here, you may add other properties (e.g. an owner) to limit the number of possible matches.

Related

How to populate recyclerview with data from parse server?

Is it possible to retrieve all the data present in a specific class in the parse server in the form of a news feed similar to facebook using recyclerView?
in order to do it you need to do the following:
I recommend you to use Parse SDK for android. You can read the docs in here . This SDK will make your life easy with performing all CRUD operations in front of your parse server instance. So the thing that you need to do is to execute query on your class like the following (taken from parse server docs):
ParseQuery<ParseObject> query = ParseQuery.getQuery("<YOUR_CLASS_NAME>");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
Log.d("score", "Retrieved " + scoreList.size() + " scores");
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
From the code above you can see that we first create a query object on class and then use findInBackground in order to retrieve all objects.
Inside that findInBackground callback what you need to do is to iterate on all results and map them to your recyclerView adapter and finally refresh it and see the results
From the answer above you can see that's its pretty simple task if you use Parse Android SDK. As much as I know there is no out of the box solution that will create recyclerView from parse query results.

ParseQuery finding object null

I am using the following code to search through my Data Browser. The columns are Location and Weather and the class is called Nowcast.
If my Data Browser has New York in it and the weather "sunny", the TextView showWeather displays "The weather in New York is sunny".
However, if I search Los Angeles and it is not in the Data Browser, it shows "The weather in Los Angeles is null" whereas it should not enter the if condition at all according to the code. What am I doing wrong?
public void getData(){
searchText = searchTextView.getText().toString();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Nowcast");
query.whereEqualTo("Location", searchText);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> object, ParseException e) {
if (e == null) {
for (ParseObject weatherObject : object) {
weatherData = weatherObject.getString("Weather");
}
showWeather.setText("The weather in " + searchTextToString + " is " + weatherData);
} else {
Toast.makeText(NowcastSearch.this, "No such location found",Toast.LENGTH_LONG).show();
}
}
});
}
whereas it should not enter the if condition at all according to the code
It shouldn't enter only if the ParseException isn't null. In other words it shouldn't enter the if condition (the first branch of it) only if there's been a problem with your query (e.g. you passed the wrong type of argument for a field condition).
There's no problem with your query. So no exception has been thrown. The query in which not a single record matches your conditions can still be a valid query. The object list is just empty but there hasn't been any errors with the whole process.
ParseException e
this exception is represeting if there is a problem while parsing whole data to objects. So you can check if parsing operation is successful or not by null checking it.
There was no exception parsing your data. That means you dont have corrupted datas or something another.
You are just getting null object because there is no data about it. You should null check your "weatherData".

Android Parse.com query user by pointer

I have a problem querying the data of a user using pointer, so this is my tables (class):
_User
objectId<string> | username<string> | ...
Posts
objectId<string> | post<string> | writerId <pointer>(_User)
The writerId contains the user id of the writer of the post.
I'm showing the posts in a custom ListView in this way:
variables_posts = new ArrayList<VariablesPosts>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.selectKeys(Arrays.asList("post"));
query.include("writerId");
on = query.find();
for (ParseObject post : on) {
VariablesPosts map = new VariablesPosts();
map.setWriter((String) post.get("username"));
map.setPost((String) post.get("post"));
variables_posts.add(map);}
The problem is that the writerId is a pointer and not a string if I put
map.setWriter((String) post.get("writerId"));
The app will crash, so I used from another help the line query.include("writerId"); to directly get the username but this line:
map.setwriter((String) post.get("username"));
is crashing the app too, when I delete it the app works fine, so in my case how can I get the username from User class by the pointer writerId?
I don't know exact Java syntax, but when you include writerId and then want to get username of that writerId User, you must first get the User object from Post's writerId .. something like:
(String)((ParseUser)post.get("writerId")).get("username")
Your naming of the pointer column is a little misleading as the column does not contain IDs. You should call it writer instead.
To get the username, do this:
query.include("writerId");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> postList, ParseException e) {
if (e == null) {
for (ParseObject post : postList) {
ParseObject writer = post.getParseObject("writerId");
String userName = writer.getString("username");
// .. (do whatever with userName)
}
} else {
Log.d("post", "Error: " + e.getMessage());
}
}
});

Android parse.com setObjectId

How set objectId with Parse.com with primary key.Can you help me ?
When i create new row, i want setObjectId of row.
final ParseObject parseObject = new ParseObject(ChapsModel.PARSE_OBJECT);
parseObject.put(ChapsModel.PARSE_FIELD_NAME_CHAPS,
chapsModel.get(i)
.getNamChap());
parseObject.put(ChapsModel.PARSE_FIELD_LINK_CHAP,
chapsModel.get(i)
.getLinkChap());
parseObject.put(ChapsModel.PARSE_FILED_TEAM_TRANSLATE,
chapsModel.get(i)
.getTeamTranslate());
parseObject.put(ChapsModel.PARSE_FIELD_OBJECT_MANGA,
ParseObject.createWithoutData(MangaModel.PARSE_OBJECT,
chapsModel.get(i)
.getObjectManga()));
parseObject.saveInBackground(new SaveCallback() {
#Override
public void done(final ParseException e) {
if (e == null) {
Log.e(">>>>>",
"done" + ojectId);
// parseObject.setObjectId(ojectId);
} else {
Log.e(">>>>>",
"else" + e.getMessage());
}
}
});
Log
java.lang.RuntimeException: objectIds cannot be changed in offline mode.
Log:
Sorry my english. thank
It's not possible to set objectId with Parse.com with primary key.
Although parse.com doesn't allow us to set the objectId of a row, you can create a new column named anything you want and you can set that new column to any objectId you want. For example, you can create a new column named myObjectId and set it to a string.
From the parse.com website at https://www.parse.com/docs/js/guide#cloud_code
The Data Browser
The Data Browser is the web UI where you can update and create objects in each of your apps. Here, you can see the raw JSON values that are saved that represents each object in your class.
When using the interface, keep in mind the following:
The objectId, createdAt, updatedAt fields cannot be edited (these are set automatically).

how to Parse Query search case insensitive in android?

I am using the parse cloud to store data .
in this case I have a table "MyUser" in this table I want to search user by name in case insensitive.
Search is working fine but the search operation is not case insensitive
I have to search case insensitive
here is my code.:-
ParseQuery<ParseObject> query = ParseQuery.getQuery("BetsUpUser");
query.whereContains("name", searchData);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objList,ParseException e) {
if (e == null) {
Log.d("score","####Retrieved " + objList.size()+ " scores");
} else {
Log.d("score", "###Error: " + e.getMessage());
}
}
});
Thanks in advance.
I Solve the problem of case insensitive string Parse Query
replace the line
query.whereContains("name", searchData);
with
query.whereMatches("name", "("+searchData+")", "i");
I also came across with this question and realized there are no much ways to do this simple thing. Another interesting approach from #ardrian by using JavaScript. For Java it will almost the same. He suggests to create one more field with lowercase version of the searchable one. It look like below:
ParseObject gameScore = new ParseObject("BetsUpUser");
gameScore.put("name", name);
gameScore.put("name_lowercase", name.toLowerCase());
gameScore.saveInBackground();
And while searching we transform input text to lower case. This way we still can use whereContains() method and it will not affect us much.
ParseQuery<ParseObject> query = ParseQuery.getQuery("BetsUpUser");
query.whereContains("name_lowercase", searchData.toLowerCase()); // the only one change
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objList,ParseException e) {
if (e == null) {
Log.d("ParseQuery", objList.get(0).get("name"));//name in actual case
} else {
Log.d("ParseQuery", e.getMessage());
}
}
});
See this post on the Parse Blog about implementing scalable search.
http://blog.parse.com/2013/03/19/implementing-scalable-search-on-a-nosql-backend/
Basically you can use an afterSave event to save a searchable field (i.e. lower-cased name) on the object.

Categories

Resources