在Parse中选择最多,第二,最第三......最近的用户

时间:2014-08-16 10:16:14

标签: java android android-layout android-activity parse-platform

我想从解析中检索用户时执行以下操作:

1)我只想为该活动页面检索一个(我猜是query.setLimit(1)

2)我只想检索最近的一个(我想查询.orderByDescending("名称");可以)

在另一个活动页面上,以及它变得复杂的地方

3)我想要第二个最近的用户

另一个活动页面

4)我想检索第三个最近的用户

在另一个活动页面上 5)我想检索第4个最新用户

在另一个活动页面上 6)我想检索第5个最新用户

最后,如果在列表中找不到用户,我希望他们将他们引导到另一个活动页面

以下是活动代码

public class Fragment1 extends Fragment {

    private String currentUserId;
    private ArrayAdapter<String> namesArrayAdapter;
    private ArrayList<String> names;
    private ListView usersListView;
    private Button logoutButton;
    String userGender = ParseUser.getCurrentUser().getString("Gender");
    String activityName = ParseUser.getCurrentUser().getString("ActivityName");
    Number maxDistance = ParseUser.getCurrentUser().getNumber("Maximum_Distance");
    String userLookingGender = ParseUser.getCurrentUser().getString("Looking_Gender");
    Number minimumAge = ParseUser.getCurrentUser().getNumber("Minimum_Age");
    Number maximumAge = ParseUser.getCurrentUser().getNumber("Maximum_Age");
    Number userage = ParseUser.getCurrentUser().getNumber("Age");

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setConversationsList();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1_layout, container, false);


        return view;
    }

private void setConversationsList() {
    currentUserId = ParseUser.getCurrentUser().getObjectId();
    names = new ArrayList<String>();
    // String userActivitySelectionName = null;

    ParseQuery<ParseUser> query = ParseUser.getQuery();

    //  query.whereEqualTo("ActivityName",userActivitySelectionName);

    query.whereNotEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
    // users with Gender = currentUser.Looking_Gender
    query.whereEqualTo("Gender", userLookingGender);
    // users with Looking_Gender = currentUser.Gender
    query.whereEqualTo("Looking_Gender", userGender);
    query.setLimit(1);
    query.whereEqualTo("ActivityName", activityName);
    //query.whereGreaterThanOrEqualTo("Age", minimumAge);
    //query.whereLessThanOrEqualTo("Age", maximumAge);
    query.orderByDescending("Name");




    query.findInBackground(new FindCallback<ParseUser>() {

        public void done(List<ParseUser> userList, ParseException e) {
            if (e == null) {
                for (int i=0; i<userList.size(); i++) {
                    names.add(userList.get(i).get("Name").toString());
                    names.add(userList.get(i).get("Headline").toString());
                    names.add(userList.get(i).get("Age").toString());
                    names.add(userList.get(i).get("ActivityName").toString());


                    //       names.add(userList.get(i).getParseObject("ProfilePicture").;


                }




                usersListView = (ListView)getActivity().findViewById(R.id.userlistview);
                namesArrayAdapter =
                        new ArrayAdapter<String>(getActivity().getApplicationContext(),
                                R.layout.user_list_item, names);
                usersListView.setAdapter(namesArrayAdapter);

                usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int i, long l) {
                        openConversation(names, i);
                    }
                });

            } else {
                Toast.makeText(getActivity().getApplicationContext(),
                        "Error loading user list",
                        Toast.LENGTH_LONG).show();
            }
        }
    });
}

public void openConversation(ArrayList<String> names, int pos) {
    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereEqualTo("Name", names.get(pos));
    query.findInBackground(new FindCallback<ParseUser>() {
        public void done(List<ParseUser> user, ParseException e) {
            if (e == null) {
                Intent intent = new Intent(getActivity().getApplicationContext(), MessagingActivity.class);
                intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
                startActivity(intent);
            } else {
                Toast.makeText(getActivity().getApplicationContext(),
                        "Error finding that user",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
}
}

提前致谢,一切顺利。

1 个答案:

答案 0 :(得分:1)

Parse有一个名为.setSkip(int x)的方法 - 该参数将跳过第一个“x”结果。如果您正在寻找最近的“第n个”用户,请设置x =(n-1):

query.setSkip(1) // will skip the first result, giving the 2nd most recent user
query.setSkip(2) //will skip the first 2 results, giving the 3rd most recent user

...等

另外,如果你想要最新的用户,Parse会提供一个查询方法.findFirstInBackground:

ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
query.whereEqualTo("playerEmail", "dstemkoski@example.com");
query.getFirstInBackground(new GetCallback<ParseObject>() {
  public void done(ParseObject object, ParseException e) {
    if (object == null) {
      Log.d("score", "The getFirst request failed.");
    } else {
      Log.d("score", "Retrieved the object.");
    }
  }
});