ParseQuery finding object null - android

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".

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.

Android parse.com How to fetch pointer type data for specific row

In My Android application i used parse.com for database,
In parse.com i having one table named : "UserInfo"
this table having one column type pointer that point to "UserImages" table
like : "userImages" Pointer {UserImages}
In "UserImages" table having one column named "profilePicture" that having all pics file.
My question is that , i fetch one row of table "UserInfo" but i don't know how to fetch image of this row because it point to another table.
I fetch another row data successfully , but face problem in fetching this "userImages" , Please help me to how to get this perticular row image from the this table that point to another table.
Thank you.
I need more info then i also provide.
Try this Code..
first get parse object (pointer) and then get image.
ParseQuery<ParseObject> offer = new ParseQuery<ParseObject>("className");
offer.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
// success
for (ParseObject parseObject : objects) {
ParseObject image = parseObject.getParseObject("UserImages");
// this is your pointer class
ParseFile pf = (ParseFile) image.get("imagename");
Log.e("image ", "URL ----- " + pf.getUrl());
}
Log.e("TAG ", "get parse pointer object " + Places.size());
} else {
// fail
}
}
});

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());
}
}
});

Parse: how to get pinned object without id

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.

How to auto_increment a field or index in my Parse class?

I want something like
objectid id name lastname pic
hx5w887 1 name1 lastname1 pic1
lops4wus 2 name2 lastname2 pic2
zh7w8sa 3 name3 lastname3 pic3
I don't want to change the objectId, just I want that field and every time I save an object increment in 1. I am searched a lot in google, about this, it is no possible at least you can something with Cloud Parse code, but I do not know how to make this function, I don't know if "Increment" can help me with this, and I do not know how to run the function anyway.
Parse.Cloud.afterSave("counter", function(request) {
var nameId = request.object.get("name").id;
var Name = Parse.Object.extend("Name");
var query = new Parse.Query(Name);
query.get(nameId).then(function(post) {
post.increment("idkey",+1);
post.save();
}, function(error) {
throw "Got an error " + error.code + " : " + error.message;
});
});
I deploy and
call the function in Android
ParseCloud.callFunctionInBackground("counter", new HashMap<String, Object>(), new FunctionCallback<String>() {
// #Override
public void done(String result, ParseException e) {
if (e == null) {
} else {
// handleError();
}
}
});
But nothing happens, what can be the problem? Sorry my bad english.
You can use ParseCloud 'beforeSave' functionality.
You can declare a code which will run before saving a new object of a specific class.
In this code you query for your class items, order it and get the first item (the highest value) then you can the next value (highest +1) to the new saved object.
For more info you can take a look at Parse documentation and in this thread (it is not in java but it is very similar)
EDIT: Since Parse is not longer is now an open source might be that things have changed.

Categories

Resources