简体   繁体   中英

Facebook SDK 3: how to sort friends list alphabetically (Android)

Using the Facebook SDK 3 , I am getting the list of GraphUser of my friends list, but when I put it into a custom ListView everyone is out of order.

Does the SDK have a way to sort the list by first name alphabetically? I have tried to do Collections.sort but it doesn't recognize GraphUser .

HELP!

Get FB Friends and put them in List GraphUser, which is then passed to ArrayAdapter

private List<GraphUser> fbFriends;

private void requestFacebookFriends(Session session) {
    Request friendsRequest = createRequest(session);
    friendsRequest.setCallback(new Request.Callback() {
        @Override
        public void onCompleted(Response response) {
            fbFriends = getResults(response);
            FriendsListAdapter adapter = new FriendsListAdapter(PickFriendActivity.this, R.layout.listview_row_friend, fbFriends);
            listView.setAdapter(adapter);
        }
    });
    friendsRequest.executeAsync();
}

private Request createRequest(Session session) {
    Request request = Request.newGraphPathRequest(session, "me/friends", null);

    Set<String> fields = new HashSet<String>();
    String[] requiredFields = new String[] {"id", "name", "picture"};
    fields.addAll(Arrays.asList(requiredFields));

    Bundle parameters = request.getParameters();
    parameters.putString("fields", TextUtils.join(",", fields));
    request.setParameters(parameters);

    return request;
}

private List<GraphUser> getResults(Response response) {
    GraphMultiResult multiResult = response.getGraphObjectAs(GraphMultiResult.class);
    GraphObjectList<GraphObject> data = multiResult.getData();
    return data.castToListOf(GraphUser.class);
}

UPDATED w/ ANSWER

private class FriendComparator implements Comparator<Friend> {
        @Override
        public int compare(Friend user1, Friend user2) {
            return user1.getFbName().compareTo(user2.getFbName());
        }
    }

Collections.sort(fbFriends, new FriendComparator());

使用具有比较器对象的Collection.sort,并为GraphUsers定义一个比较器,以比较名称。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM