Hi i try to retrieve data from 2 different tables in android using parse but with no success. I want to retrieve "titolo" from "Luoghi" table for all my "imageFile" in Photos table. This is my
databse and this is my code
list = new ArrayList<>();
try {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Photos");
query.include("luogoPointer");
listaEventiParse = query.find();
for (ParseObject country : listaEventiParse) {
// Locate images in flag column
ParseFile image = (ParseFile) country.get("imageFile");
ParseObject luoghi= country.getParseObject("luogoPointer");
ListaEventiItem map = new ListaEventiItem();
map.setImmagine(image.getUrl());
map.setId(luoghi.getString("titolo"));
list.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
i got java.lang.NullPointerException on luoghi.getString("titolo").
what i am missing?
You included luogoPointer in your query but many entries in your Photos table for this pointer is blank. When you get Photos result then you got null pointer for many records and calling getString() on this null pointer, that's why you are getting NullPointerException.
Include one more condition in your query to select only that record which contain luogoPointer.
query.whereExists("luogoPointer");
Now your query is like this :
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Photos");
query.include("luogoPointer");
query.whereExists("luogoPointer");
listaEventiParse = query.find();
Instead of including only the pointer to include the field (or fields) you want to retrieve before the .find() call:
query.include("luogoPointer.titolo");
Related
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
}
}
});
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());
}
}
});
How to check if table is empty using parse , I'm having a problem with the code below :
private String[] getMaxDateMessage() throws ParseException {
final String[] msgData = new String[3];
ParseObject ob = null;
String[] userIds = {currentUserId, recipientId};
ParseQuery<ParseObject> query = ParseQuery.getQuery("ParseMessage");
query.whereContainedIn("senderId", Arrays.asList(userIds));
query.whereContainedIn("recipientId", Arrays.asList(userIds));
query.orderByDescending("createdAt");
if(query.hasCachedResult())
{
ob = query.getFirst();
if (ob.isDataAvailable()) {
//for (int i = 0; i < 1; i++) {
//createdDate[0] = messageList.get(i).get("createdAt").toString();
msgData[0] = ob.getCreatedAt().toString();
msgData[1] = ob.get("senderId").toString();
msgData[2] = ob.get("recipientId").toString();
// }
}
}
The thing is that the table is empty , so the query should return null , but no exception is been throwed , it just crashes the app .
So how can I check if the table is empty before trying to fetch any data ?
Update : The solution that I have found is to use query.count().
If the count returns a value that is not 0 then the table is not empty .
Using query.count() to determine if the table is empty is not an optimal solution. While this is perfectly fine when actually run against an empty table, using query.count() will almost always result in a sub-optimal query when there's more than one object in the table. The reason for this is quite clear: you only care about the first object matched by this query, yet a query.count() will scan the whole table in order to return the total of objects that match your query.
Therefore, the ideal solution is to simply use query.getFirst() and check if you get any results. You should be able to handle the case where ob is not a ParseObject, e.g. the collection is either empty or no objects match your query.
I have made a table in Parse.com using Dashboard name Blogs. I have made a column url type String using Dashboard inside it where I have to post blogs url and I have posted some url inside this column by adding rows. Now I am trying to get these values inside my application by using this code in onCreate() method.
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Blogs");
parseQuery.whereEqualTo("url", true);
List<ParseObject> objects = null;
try {
objects = parseQuery.find();
}
catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
I am getting objects of size 0. I am unable to understand how to get my url column all values.
Read the api docs carefully.
I believe you want to query if the urls exists in the column. (since you have used parseQuery.whereEqualTo("url", true);)
From the docs,
public ParseQuery<T> whereEqualTo(String key,
Object value)
What it does : Add a constraint to the query that requires a particular key's value to be equal to the provided value.
So in this case for url, the value should be a String (which you want to match to) if you have defined the column as String. Putting value as boolean will give you undesired result.
If you want to check if the column url exists for the object,
then use whereExists(String key).
Use like this
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Blogs");
parseQuery.whereEqualTo("url","true");
List<ParseObject> objects = null;
try
{
objects = parseQuery.find();
}
catch (ParseException e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
I am new to parse.com and databases in general.
For my android app, I need to search if an object is available and if so, then it should give me its price. So my class is vegetables, and it has a column called 'isAvailable' and another column called 'price'
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("vegetables");
query.whereEqualTo("isAvailable", true); try {
ob = query.find();
}
catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
So, now I do have a list of all the vegetables that are available, but how do I query the price?
I was thinking of using the whereMatchesQuery(key, query) but it doesnt make too much sense on how to use it.
Could you guide me so as to what I should be doing, thanks !
So now you'll have a list of vegetables objects in ob.
Just iterate through ob pulling the price from each one in the list.
for(ParseObject vegetable : ob){
Log.d("TAG", "price: " + vegetable.getDouble("price"))
}