how to get data from separate columns in parse.com - android

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

Related

Can I use Shared Preference rather SQLite Database?

I want to keep data with shared preference. I looked some examples on the internet but I couldn't answer my question. I can use database for my purpose but it is really hard especially in android. There are examples that has more than 10 class to just create database. Why is it hard ?. It is cons of the db in android.
Anyway, I will keep just 1 table that has 4(relational) columns in my application.
id | name | surname |phone number.
Can I use Shared Preference to achieve this goal? How to use for 4 different columns?
You should use Database for this,
But if you will have only one entry in each column then you can use shared preference
Database is not that hard, check below page
http://www.tutorialspoint.com/android/android_sqlite_database.htm
DataBase Meet your needs ,if you want to easily use DataBase ,try xUtils, ------some code about xUtils:
//add
User user1 = new User();
user1.setAge(40);
user1.setName("bob");
user1.setPhone("123456789");
ArrayList<User> list = new ArrayList<>();
list.add(user1);
try {
MyApplication.dbUtils.saveAll(list);
} catch (DbException e) {
e.printStackTrace();
Log.d("TAG",e.getMessage());
}
//updata---1
try {
User users = MyApplication.dbUtils.findById(User.class,1);
users.setAge(18);
MyApplication.dbUtils.update(users,"age");
} catch (DbException e) {
e.printStackTrace();
}
//delete
User user = new User();
user.setId(3);
try {
MyApplication.dbUtils.delete(user);
} catch (DbException e) {
e.printStackTrace();
}
//query
List<User> allUsers = MyApplication.dbUtils.findAll(Selector.from(User.class)
.where("age",">","20")
.and("age","<","50"));

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

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.

Retrive value of a column in parse.com

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

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