回调时间

时间:2014-11-25 17:34:23

标签: java android callback

嘿大家即将到来的火鸡日快乐!所以我试图实现一个搜索管理器类,使用解析后端处理所有搜索这个Android应用程序并遇到一个有趣的问题。以下是应该调用搜索管理器搜索功能的方法

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。

一如既往地感谢所有的帮助!

0 个答案:

没有答案