i have a query that retrieves a parse objects:
i tried to debug the callback function, and i found that i am getting a correct number of objects but all of their values are null.
allthough there is a value called "state" in which i am getting the values
what am i doing wrong? how do i get the values properly?
final List<Kehila> newKehilas = new ArrayList<>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Kehila");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e != null) {
Log.e("UNITE", e.toString());
} else {
for (ParseObject k : list) {
Kehila newKehila = new Kehila();
newKehila.setObjectId(k.getString("objectId"));
newKehila.setName(k.getString("name"));
newKehila.setCity(k.getString("city"));
newKehila.setNeighborhood(k.getString("neighborhood"));
newKehila.setStreet(k.getString("street"));
newKehila.setHouseNumber(k.getInt("houseNumber"));
newKehilas.add(newKehila);
}
}
}
});
return newKehilas;
I think you need to call save() or saveEventually() on your new objects.
Related
I am trying to populate an Array from a parse.com query and I am stock in the part of getting the element from query. basically i dont know where to get the current location for exercise_object.get(location) thanks.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Programs");
query.whereEqualTo("name", objname);
query.orderByAscending("name");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
//success
String [] exercice = new String[3];
exercise_object = list;
Toast.makeText(Wko_Display.this, "Workouts", Toast.LENGTH_LONG).show();
ParseObject program_object = exercise_object.get(location);
exercice [0] = exercise_object.getString("wk0");
exercice [1] = exercise_object.getString("wk1");
exercice [2] = exercise_object.getString("wk2");
}
If I understand what you're trying to do correctly, then this should work:
#Override
public void done(List<ParseObject> list, ParseException e){
if (e == null){
for (ParseObject pObj : list){
String[] exercise = new String[3];
// assuming each ParseObject has a pointer to a Program object in the database
ParseObject programObject = pObj.getParseObject("location");
// do whatever you want with this programObject
// then do any further stuff with the current pObj in the list
exercise[0] = pObj.getString("wk0");
// ...
}
} else{
Log.e(TAG, e.getMessage());
}
}
This would be wrong if you're expecting only one object to be returned from the query though. Let me know.
On Parse.com, I have a class named ImagesEntry with a column named ImageFile that has images saved.
I have this code on android to read every row and return the images but it returns null.
ParseFile[] getImagesFromParseCloudDB() {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"ImagesEntry");
query.orderByDescending("_created_at");
try {
ob = query.find(); // 'List<ParseObject> ob' initialzed above OnCreate
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
int i = 0;
for (ParseObject images : ob) {
// imagesFromDB is an array of type 'ParseFile'
imagesFromDB[i] = (ParseFile)images.getParseFile("ImageFile");
i++;
}
return imagesFromDB;
}
Any pointers on what wrong am I doing ?
I think the reason for this is that getParseFile() doesn't request actual file. According to docs
function will not perform a network request
You need to add call ParseFile.getDataInBackground() or ParseFile.getData() to get actual data of the file. But don't try to spawn too many background loading tasks while iterating through array - you'll get exception if your array's size would larger than PoolSize of ExecutorService used by Parse. Consider creating some background task and synchronous execution of getData() request or customizing ExecutorService for Parse.
First of all, this string "_created_at" is wrong for me. In my app I have "createdAt" and works fine.
I recommend you first get all ID in a List and use my code (it works for me):
ParseQuery query = new ParseQuery("Your_class_name");
query.getInBackground(imagenID, new GetCallback() {
#Override
public void done(ParseObject parseObject, ParseException e) {
}
#Override
public void done(Object o, Throwable throwable) {
ParseObject object = (ParseObject) o;
ParseFile foto = (ParseFile) object.get("Your_column_name");
foto.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] bytes, ParseException e) {
if (e == null) {
//do things with this bytes
} else {
Toast.makeText(getApplicationContext(), "Error al descargar la foto", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
}
});
}
I have to fetch some data from the parse cloud. I'm using the parse sdk for android. How do I call the method findInBackground asynchronously in my activity? Essentially, where do I place this piece of code in my Activity? Below is my code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Average");
query.whereEqualTo("squarefeet", area);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
for (int i = 0; i < objects.size(); i++) {
ParseObject p = objects.get(i);
averageConsumption = p
.getNumber("average_consumption");
efficientConsumption = p
.getNumber("efficient_consumption");
}
}
} else {
// something went wrong!
}
}
});
I called it inside an OnClick method of an OnClickListener. This made it asynchronous! Pretty silly but works! Previously I called it synchronously in OnCreate.
In my fragment, I have declared the food variable
public class EatingFragment extends Fragment {
...
static int food = 0;
}
I load the variable from a parse.com database. Inside the onCreateView() function, I call the function loadSwallowCount(), implemented as below
void loadSwallowCount() {
SharedPreferences pref = this.getActivity().getApplicationContext().getSharedPreferences("MyPref", 0);
String id = pref.getString("parse_object_id", "empty");
if (id == "empty") {
food = 0;
Log.d("Not stored"," Swallow count not stored on device");
}
else {
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserData");
query.getInBackground(id, new GetCallback<ParseObject>() {
public void done(ParseObject onlineData, ParseException e) {
if (e == null) {
food = onlineData.getInt("SwallowCount");
Log.d("mes", "food is " + food);
}
}
});
}
}
When I test the food count inside the loadSwallowCount function, I get the proper value. However, after the function returns to onCreateView(), I do another log, and strangely the value of food is 0.
loadSwallowCount is a function that use the method call getInBackground, which has a asyncronous callback in new GetCallback<ParseObject>.
This means that your code reach your log in onCreateView before the callback is called.
In this case, you will be able to use your food variable only after the callback is executed.
try this:
public void done(ParseObject onlineData, ParseException e) {
if (e == null) {
food = onlineData.getInt("SwallowCount");
processFoodValue();
}
}
I have a class named UserData on parse.com, containing column name as usersports which is of array type,i successfully added strings in the array, but i got stuck while fetching the array values and displaying to a list.
say: parse array type is like this,
["Cycling","Diving","Equestrian"]
I have tested using JsonArray for parsing the array values from parse but not succeed. In my code getting checkuserSportarray as null. Please help me out.
Below is my code:
private void userDetailfromParse(){
ParseQuery<ParseObject> query = ParseQuery.getQuery(Sportapp.USERDATA);
query.whereEqualTo(Sportapp.USER_GOOGLE_ID, google_id_from_preference.trim());
query.getFirstInBackground(new GetCallback<ParseObject>() {
#SuppressWarnings("unchecked")
public void done(final ParseObject login_data, ParseException e) {
if (login_data == null) {
Log.d("Data", "The getFirst request failed. in profile"+e.getCode());
}
else{
userEmail = login_data.getString(Sportapp.USER_EMAIL);
userName = login_data.getString(Sportapp.USER_NAME);
userGender = login_data.getString(Sportapp.USER_GENDER);
checkuserSportarray = login_data.getJSONArray(Sportapp.USERDATA);
}
}
});
}
works fine for me:
List<String> list11 = new ArrayList<String>();
ParseQuery<ParseObject> pQuery = ParseQuery.getQuery("UserData");
pQuery.whereEqualTo(Sportapp.USER_GOOGLE_ID, google_id_from_preference.trim());
pQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e==null) {
if (list.size()>0) {
ParseObject p = list.get(0);
if (p.getList("usersports")!=null) {
list11 = p.getList("usersports");
}
else
{
list11= null;
}
}}
}
});
use getList method to get the data from array column of parse table
now if you want to get all individual data of parsed array ,you can simply apply looping on **list11**.
For more info see this link: Parse Object
pass your column name in getJSONArray method like this:-
checkuserSportarray = login_data.getJSONArray("usersports");