public void SearchForLostItems(){
String searchtext = mSearchView.getQuery().toString();
List<ParseObject> foundposts = mSearchManager.BasicSearch(searchtext);
//if we found a matching post go to the post details
if(parseObjects != null){
//Try to get our found posts count if there is more than one match than list them for the user and let them browse
try {
//if there is only one match then go ahead to the post
if(parseObjects.size() == 1){
ParseObject foundpost = parseObjects.get(0);
Post post = new Post(foundpost.getObjectId(), "");
GoToFoundPost(post);
}
} catch (ArrayIndexOutOfBoundsException ex) {
e.printStackTrace();
}
}
}
else
{
e.printStackTrace();
}
}
});
}
这是搜索管理器中的实现
//Basic search for when someone searches by text, should accept some search text and returhn amatched list of parse objects
public List<ParseObject>BasicSearch(String searchtext){
final List<ParseObject> PostsHolder = null;
//Do a search for all posts that contain the search text
ParseQuery<ParseObject> foundposts = new ParseQuery<ParseObject>(ParseConstants.CLASS_POSTS); // get the whole class
foundposts.whereEqualTo(ParseConstants.POST_TITLE,searchtext); // filter the results based on the search text
foundposts.findInBackground(new FindCallback<ParseObject>() { //call the find in background routine to convert the results to a list
@Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if(e == null){
PostsHolder.addAll(parseObjects);
Log.d("Posts Added", "true");
}
else
{
e.printStackTrace();
}
}
});
返回PostsHolder;
}
我认为这个问题是因为函数在回调完成之前返回但是我不确定java和特别是android。
一如既往地感谢所有的帮助!