Retrive value of a column in parse.com - android

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

Related

How can I put an array as the argument in OrmLite where clause?

I want to pass a few dates to this method, and get a few objects from the DB in return.
QueryBuilder<WorkDayDB, Long> queryBuilder =
application.ormLiteDatabaseHelper.getWorkDayDBDao().queryBuilder();
Where where = queryBuilder.where();
try {
where.eq("date", dates);
return queryBuilder.prepare();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
However, the where.eq("date", dates); throws an exception, saying it needs a single Date, not an array of dates.
Apparently eq means equals which is specifically used for a single argument, whereas in (which is another method of where in OrmLite) allows for an array of arguments to be passed. This solved my problem.

How to retrieve data in parse from 2 different tables android?

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

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

Parse fetchIfNeeded() not working

I have 2 pointer objects(pointing to ParseUser) in my table "Attack".It seems these pointer objects take a while to get retrieved.Hence my code directly didnt work and gave me the exception:
java.lang.IllegalStateException: ParseObject has no data for this key. Call fetchIfNeeded() to get the data.
I then did the needful surrounding the fetchIfNeeded function with a try-catch block:
ParseObject battle = null;
try {
battle = objects.get(0).fetchIfNeeded();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ParseUser attacker1 = battle.getParseUser("Attacker");
Log.i("dontest",attacker1.getUsername());
It is still returning the same.I even checked with isDataAvailable function and it returned true.Any way around this?
P.S.: My query returns exactly 1 row which I checked with the size() function.
Here's the documentation describing fetchIfNeeded() function.
Use Your query like this..
ParseQuery<ParseObject> pQueryFromAttack = new ParseQuery<ParseObject>("Attack");
// use include to fetch details while quering----increase loading speed by doing this.
pQueryFromAttack.include("pointer to _user table");
List<ParseObject> listObj = pQueryFromAttack.find();
for eg:
String userName =listObj.get(0).fetchIfNeeded().getString("name");
Use it like this...

how to cast a ParseObject to ParseUser

Hi I was wondering how I can get a ParseUser object from a ParseObject. I need to do this because a ParseQuery returns a List. Here is my code and thank you for the help!
// get the currentUser
currentUser = ParseUser.getCurrentUser();
List<ParseObject> friendsList = currentUser.getList("friendsList");
// search for the person the user wants to add as a friend
List<ParseObject> userResults = new ArrayList<ParseObject>();
ParseQuery otherUserQuery = ParseUser.getQuery();
otherUserQuery.whereEqualTo("username", "jyo2");
try {
userResults = otherUserQuery.find();
} catch (ParseException e1) {
// fail
e1.printStackTrace();
}
// get the friend from the query if there was one and add it to the
// currentUser friends list
if (userResults.size() != 0) {
ParseObject currentObject = userResults.get(0);
friendsList.add(currentObject);
currentUser.put("friendsList", friendsList);
}
// save the update
try {
currentUser.save();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// try to view the friends
List<ParseObject> currentFL = currentUser.getList("friendsList");
ParseObject currentPU = currentFL.get(0);
System.out.println("THIS IS SIZE" + currentFL.size());
System.out.println(currentPU.get("name"));
Use the existing "User" class in Parse but subclass it using
#ParseClassName("_User")
public class PUser extends ParseUser {
As per this article. You actually specifically need to refer to the _User "ParseClassName" when subclassing this object. It's super hairy, I was stuck on this for ages because for other classes you only need to "register" the class with Parse using it's literal name as per the Parse Data Browser, which in this case is "User", "Info", "Post" etc, but the User class specifically requires the underscore
Edit Sorry, I should have mentioned, then you simply cast the subclass PUser when you get your returned objects from the query. You may need to change the query to be a User query instead of an object one too.
I know this is an old question, but this wasn't clear for me and there wasn't a complete thread on the issue I had, on SO. Hope this helped.
I don't think you need to "cast" is to ParseUser, a parseuser is a parseobject as well, apart from having some basic pre-defined properties attached to it, which you can anyways query like you'd query any other parseobject

Categories

Resources