I'm developing an android native app using Facebook SDK 3.5.
I'm trying to have an autocompletetextview where I can pick some facebook friends, for this I'm using an newMyFriendsRequest as below:
private void fbFriendsRequest(final Session session) {
Request request = Request.newMyFriendsRequest(session, new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> listFacebookFriends, Response response) {
// If the response is successful
if (session == Session.getActiveSession()) {
if (listFacebookFriends != null) {
mAdapter = new GraphUserAdapter(FriendPicker.this, listFacebookFriends);
mAutoComplete.setAdapter(mAdapter);
Toast.makeText(FriendPicker.this,"Friends Loaded",Toast.LENGTH_SHORT).show();
}
}
if (response.getError() != null) {
// Handle errors, will do so later.
}
}
});
request.executeAsync();
}
Then each time the user pick a row from the autocompletetextview I add this row to another list, and when a I press OK button I have:
public void onOkClick(View v) {
if(mGraphUsersList.size()==0) {
setResult(RESULT_CANCELED,null);
} else {
Intent returnIntent = new Intent();
Iterator<GraphUser> itr = mGraphUsersList.iterator();
int n = mGraphUsersList.size();
String[] idfb = new String[n];
String[] names = new String[n];
String[] lastnames = new String[n];
int i = 0;
while(itr.hasNext()) {
GraphUser User = (GraphUser) itr.next();
idfb[i]=User.getId();
names[i]=User.getFirstName(); // problem
lastnames[i]=User.getLastName(); // problem
i++;
}
returnIntent.putExtra("idfb",idfb);
returnIntent.putExtra("names",names);
returnIntent.putExtra("lastnames",lastnames);
setResult(RESULT_OK,returnIntent);
}
finish();
}
When I use the getFirstName() and getLastName() functions I have null strings, but if I use getName() function it's return the name+lastname. It's like if in the list given by newMyFriendsRequest something has been lost.
Anyone knows a workaround for this?
To get other fields than id
and name
of friends, you need to specify them explicitly in your request.
For this add them to the Bundle
of your request like:
Request request = Request.newMyFriendsRequest(session, new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> listFacebookFriends, Response response) {
// your code like you did
}
}
// here add fields explicitly
Bundle bundle = request.getParameters();
mBundle.putString("fields", "id,first_name,last_name");
// execute like you did
request.executeAsync();
This is one solution for you, or another one is to use this super simple library: android-simple-facebook
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With