Parse Findinbackground Doesn't Add To Global Arraylist?
Solution 1:
String[] items;
final ArrayList<String>tempListItems;
{
tempListItems= newArrayList<>();
}
publicvoidinitList() {
if(query()){
items = newString[tempListItems.size()];
items = tempListItems.toArray(items);
Arrays.sort(items);
Log.d("HSearch - initList", "Generate Clean List");
Log.d("initList - temp size", Integer.toString(tempListItems.size()));
Log.d("initList - items size", Integer.toString(items.length));
}
}
privatebooleanquery() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Table");
query.orderByAscending("name");
query.findInBackground(newFindCallback<ParseObject>() {
@Overridepublicvoiddone(List<ParseObject> list, com.parse.ParseException e) {
if (e == null) {
for (ParseObject name : list) {
tempListItems.add(name.getString("name"));
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
returntrue;
}
Changed your code, this should work, as It worked for me.
Make your methods public to use in other classes.
Solution 2:
How about creating a Handler before query.findInBackground :
Handlerhandler=newHandler();
and put the for loop inside handler.post(Runnable) :
handler.post(new Runnable(){
@Override
run(){
for (ParseObject name : list)
tempListItems.add(name.getString("name"));
}
});
Solution 3:
As see here:
findInBackground : Retrieves a list of ParseObjects that satisfy this query from the source in a background thread.
Means findInBackground
method is running on different Thread from which calling query.findInBackground
method.
if I check the size of tempListItems after done() the size is not empty
Here:
if(query()){
//....
}
if we check size of tempListItems
inside if-block
then size always zero
because if block is executing just after starting query.findInBackground
Thread without waiting for Result of Task.
done
is a callback method which is called when findInBackground
executing complete on same Thread which is started it.
Solution 4:
query()
will return true
without waiting of done
response. that's why list
is empty.
put your code in
done
success
privatevoidquery() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Table");
query.orderByAscending("name");
query.findInBackground(newFindCallback<ParseObject>() {
@Overridepublicvoiddone(List<ParseObject> list, com.parse.ParseException e) {
if (e == null) {
for (ParseObject name : list) {
tempListItems.add(name.getString("name"));
}
items = newString[tempListItems.size()];
items = tempListItems.toArray(items);
Arrays.sort(items);
Log.d("HSearch - initList", "Generate Clean List");
Log.d("initList - temp size",
Integer.toString(tempListItems.size()));
Log.d("initList - items size",
Integer.toString(items.length));
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
Post a Comment for "Parse Findinbackground Doesn't Add To Global Arraylist?"