Facebook SDK 3:如何按字母顺序排列好友列表(Android)

时间:2014-02-23 01:22:59

标签: android facebook sorting facebook-graph-api

使用Facebook SDK 3,我会收到朋友列表GraphUser的列表,但是当我将其放入自定义ListView时,每个人都会出现故障。

SDK是否可以按字母顺序按名字对列表进行排序?我曾尝试Collections.sort,但无法识别GraphUser

HELP!

获取FB好友并将其放入List GraphUser,然后传递给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);
}

更新了答案

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());

1 个答案:

答案 0 :(得分:0)

使用Collection.sort获取比较器对象,并为GraphUsers定义比较名称的比较器。