Android - Parse.com Populating Array from ONE element - android

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.

Related

Retrieving Array from parse server and displaying the data in a list view

I'm new to android and I need help in retrieving the array that I created in parse server named as "busStops" but I can't figure out where the problem exists in my code
ParseQuery<ParseObject> parseQuery = new ParseQuery<ParseObject>
("RouteDetails");
parseQuery.whereEqualTo("routeNumber",searchView.getText().toString());
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> busStops, ParseException e) {
if(e == null){
final List<String> arrayList = new ArrayList<>();
for (ParseObject parseObject : busStops){
if (parseObject.getList("busStops") != null) {
arrayList.add(parseObject.getList("busStops").toString());
}
}
arrayAdapter = new ArrayAdapter(SearchForRoutes.this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter);
}
}
});
It is possible to show an Array in a ListView. You should use getJSONArray instead of getList in your JAVA code. See my code below.
My structure class is:
Then, I added the code below into my Activity (MainActivity.java):
final ListView listView = (ListView) findViewById(R.id.listviewA);
ParseQuery<ParseObject> query = ParseQuery.getQuery("RouteDetails");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> results, ParseException e) {
if (e == null) {
for(int i = 0; i < results.size(); i++) {
JSONArray array = results.get(i).getJSONArray("busStop");
for(int j = 0; j < array.length(); j++) {
try {
dataList.add(array.getString(j));
} catch (JSONException e1) {
e1.printStackTrace();
}
}
myArray = dataList.toArray(new String[dataList.size()]);
ArrayAdapter<String> adapterList
= new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, myArray);
listView.setAdapter(adapterList);
}
} else {
final Toast toast = Toast.makeText(
MainActivity.this,
String.valueOf("Error =>" + e.getMessage()),
Toast.LENGTH_LONG
);
toast.show();
}
}
});
And the result will be something like as this:
First of all, you may don't need to create another ArrayList than the list that is returned by parseObject.getList("busStops"). You can directly pass it to the adapter constructor. But since you may want to do a process on the list, iterate the list like :
for(Object s : parseObject.getList("busStops") { String current = s.toString(); }
Also you can save the list to another ArrayList by passing it to ArrayList constructor.
ArrayList<String> al = new ArrayList<>(parseObject.getList("busStops"))
And finally, you can fix your code (that is worst-practice :) ) by simply deleting the .toString() from the line in your none-null if statement and changing add to addAll.
like this : arrayList.addAll(parseObject.getList("busStops"));

Return Parse query data from an auxiliary class to my activity

I am getting data from the database, and populating the ArrayList
The variable ArrayList categories, is populated within the scope of the findInBackground. I noticed that the program never goes out of the findInBackground scope, so it never gets to return anything for my activity
public ArrayList<Categorias> onCompleteGetData(String tableName) {
ParseQuery<ParseObject> query = ParseQuery.getQuery(tableName);
query.findInBackground((objects, e) -> {
for (ParseObject objectList : objects) {
Categorias categ = new Categorias();
categ.setName(String.valueOf(objectList.get("name")));
categ.setCategoriaId(String.valueOf(objectList.get("objectId")));
this.categories.add(categ);
}
});
return this.categories;
}
['House', 'loan', 'salary']
Part of My activity
if (value == -1){
categorias = new ArrayList<>();
CategoryController controller = new CategoryController(context);
categorias = controller.onCompleteGetData(Utils.CATEGORIA_EXPENSE);
Toast.makeText(context, String.valueOf(controller.onCompleteGetData(Utils.CATEGORIA_DESPESA).size()), Toast.LENGTH_SHORT).show();
}else if (value == +1){
categorias = new ArrayList<>();
CategoryController controller = new CategoryController(context);
categorias = controller.onCompleteGetData(Utils.CATEGORIA_REVENUE);
Toast.makeText(context, String.valueOf(controller.onCompleteGetData(Utils.CATEGORIA_RECEITA).size()), Toast.LENGTH_SHORT).show();
}
Since you are using findInBackground method, "return this.categories;" is running before (and therefore returning an empty list) than your loop. The easier way to fix your code would be using the find method instead of the findInBackground.
public ArrayList<Categorias> onCompleteGetData(String tableName) {
ParseQuery<ParseObject> query = ParseQuery.getQuery(tableName);
List<ParseObject> objectList = query.find();
for (ParseObject objectList : objects) {
Categorias categ = new Categorias();
categ.setName(String.valueOf(objectList.get("name")));
categ.setCategoriaId(String.valueOf(objectList.get("objectId")));
this.categories.add(categ);
}
return this.categories;
}

android: parse object with null values

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.

Unable to retrieve relational data with object id

I am trying to retrieve a list of posts (_Relation ) that are stored as a relation in a parse class called "Threads". I tried many different usages but didn't succeed though the object Id and the getClassName methods are working I am unable to get the list.
ParseRelation<ParseObject> posts = recylerViewListAdapter.currentThreadObject.threadObject.getRelation("postMessage");
ParseQuery postsQuery = posts.getQuery();
Log.d("DEBUG_NETWORK_CALL : ", String.valueOf(postsQuery.getClassName())); // This works and I get the class name as "Posts" while debugging.
// The List size retrieved is 0 when I print it
postsQuery.findInBackground(new FindCallback() {
#Override
public void done(List list, ParseException e) {
List<postModelStore> postModelStoreArrayList = new ArrayList<postModelStore>();
Log.d("DEBUG_NETWORK_RETRIEVAL : ","DONE");
Log.d("DEBUG_NETWORK_LIST_SIZE : ",String.valueOf(list.size()));
for (int i = 0; i < list.size(); i++) {
ParseObject parseObject = (ParseObject) list.get(i);
String postMessage = (String) parseObject.get("postMessage");
Log.d("post Message", postMessage);
ParseUser parseUser = (ParseUser) parseObject.get("postedBy");
String profilePictureUrl = "local";
profilePictureUrl = ((ParseFile) parseUser.get("profilePicture")).getUrl();
postModelStore retrievedPostDetails = new postModelStore(postMessage, profilePictureUrl);
postModelStoreArrayList.add(retrievedPostDetails);
}
postListObject = postModelStoreArrayList;
}
#Override
public void done(Object o, Throwable throwable) {
callMe();
}
});

How to fetch value from parse class containing Array type column of strings in android

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

Categories

Resources