Mark as selected by user as indicated in FriendPicker (Facebook SDK for Android)

I made tutorials for the Facebook SDK for Android (especially the "Show Friends" guide ).

How can I tag selected users that I selected earlier in PickerActivity when I click the "Show Friends" button again?

+6
source share
5 answers

You must pass a comma string with the selected facebook user IDs bundled with FriendPickerFragment.

Bundle args = new Bundle(); args.putString("com.facebook.android.PickerFragment.Selection", "11111, 2222, 333, already selected facebook ids...."); friendPickerFragment = new FriendPickerFragment(args); 

In PickerFragment onActivityCreated (), it will parse the selected identifiers selected in the list. You can see the following code in PickerFragment on FacebookSDK.

 selectionStrategy.readSelectionFromBundle(savedInstanceState, SELECTION_BUNDLE_KEY); 
+2
source

I looked at the source code of the SDK for Facebook, and it seems to me that PickerFragment does not allow you to re-select previously selected items. Since the Facebook SDK is published under Apache License 2.0, and you have access to the full source code, I think you could try modifying PickerFragment so that it has the necessary methods.

+3
source

Here is a solution that worked for me. I am expanding a standard FriendPickerFragment file.

 public class FriendPickerFragment2 extends FriendPickerFragment { SelectionStrategy selectionStrategy; String mPreSelectedIDs; public FriendPickerFragment2(Bundle args) { super(args); } @Override SelectionStrategy createSelectionStrategy() { selectionStrategy = getMultiSelect() ? new MultiSelectionStrategy() : new SingleSelectionStrategy(); return selectionStrategy; } public void showInitialSelection() { Bundle bundle = new Bundle(); bundle.putString(this.MULTI_SELECT_BUNDLE_KEY, mPreSelectedIDs); selectionStrategy.readSelectionFromBundle(bundle, this.MULTI_SELECT_BUNDLE_KEY); adapter.notifyDataSetChanged(); } public void setInitialSelection(String IDs) { mPreSelectedIDs = IDs; } 

}

I use FriendPickerFragment2 as a regular FriendPickerFragment. In OnCreate, I do the following:

  FragmentManager fm = getSupportFragmentManager(); if (savedInstanceState == null) { final Bundle args = getIntent().getExtras(); friendPickerFragment = new FriendPickerFragment2(args); friendPickerFragment.setInitialSelection(pickedUsersString()); fm.beginTransaction() .add(R.id.friend_picker_fragment, friendPickerFragment) .commit(); } else { friendPickerFragment = (FriendPickerFragment2) fm.findFragmentById(R.id.friend_picker_fragment); } 

Here, selectedUsersString is the identifier code string.

The last point is to add one line to OnStart:

 protected void onStart() { super.onStart(); try { friendPickerFragment.loadData(false); friendPickerFragment.showInitialSelection(); } catch (Exception ex) { onError(ex); } } 

This solution worked for me.

+1
source

Good news. In the current SDK version (3.6), the following function was added:

  • Added setSelection methods on FriendPickerFragment to allow pre-selection of friends.
  • Added example of using setSelection API in Friend Picker sample
0
source

FriendPickerFragment has this method:

 /** * Sets the list of friends for pre selection. These friends will be selected by default. * @param userIds list of friends as ids */ public void setSelectionByIds(List<String> userIds) { preSelectedFriendIds.addAll(userIds); } 

And this method:

 public void setSelection(List<GraphUser> graphUsers) { List<String> userIds = new ArrayList<String>(); for(GraphUser graphUser: graphUsers) { userIds.add(graphUser.getId()); } setSelectionByIds(userIds); } 

You must call one of these methods immediately after creating the instance:

  friendPickerFragment = new FriendPickerFragment(args); friendPickerFragment.setSelectionByIds(fbIDs); 
0
source

All Articles