我能够获取朋友列表和关联的对象ID,但是我不知道如何将它们传递给另一个活动。当我使用意图传递数据时,在下一个活动中将得到空结果。请有人帮我。

这是我的代码

 public class MainFragment extends Fragment {

        private CallbackManager callbackManager;
        private TextView textView;
        private AccessTokenTracker accessTokenTracker;
        private ProfileTracker profileTracker;
        static ArrayList<String> friendlist = new ArrayList<String>();
        public MainFragment() {

        }


        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

            callbackManager = CallbackManager.Factory.create();

          }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_main, container, false);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
            final ProfilePictureView pictureView=(ProfilePictureView) view.findViewById(R.id.picture);;

            loginButton.setFragment(this);
            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
             {
                @Override
                public void onSuccess(LoginResult loginResult) {

                    // AccessToken accessToken = loginResult.getAccessToken();
                    Toast.makeText(getActivity().getApplicationContext(),"Login Success", Toast.LENGTH_LONG).show();
                    LoginManager.getInstance().logInWithReadPermissions(getActivity(), Arrays.asList("user_friends","user_photos","email"));
                    GraphRequest request = GraphRequest.newMeRequest(
                            AccessToken.getCurrentAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(
                                        JSONObject object,
                                        GraphResponse response) {
                                    // Application code
                                    String name = object.optString("name");
                                    String id = object.optString("id");
                                    String email = object.optString("email");
                                    Log.d("Name",name);
                                    Log.d("ID",id);
                                    Log.d("email",email);
                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,email");
                    request.setParameters(parameters);
                    request.executeAsync();

                    GraphRequestBatch batch = new GraphRequestBatch(
                            GraphRequest.newMyFriendsRequest(
                                    AccessToken.getCurrentAccessToken(),
                                    new GraphRequest.GraphJSONArrayCallback() {
                                        @Override
                                        public void onCompleted(
                                                JSONArray jsonArray,
                                                GraphResponse response) {
                                            JSONObject object=response.getJSONObject();


                                            try {
                                                JSONArray listFriends = object.getJSONArray("data");

                                                //get the entire friendlist
                                                for(int i=0;i<listFriends.length();i++)
                                                {
                                                    JSONObject firstFriend=listFriends.getJSONObject(i);

                                                    friendlist.add(firstFriend.optString("name"));
                                                    Log.d("listOfFriends", String.valueOf(friendlist));
                                                }

                                               // String friendName=firstFriend.optString("name");
                                               // Log.d("friendname",friendName);
                                                //String friendid = firstFriend.optString("id");

                                                Log.d("listFriends", listFriends.length() + "");


                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }


                                        }
                                    }),
                            GraphRequest.newMyFriendsRequest(
                                    AccessToken.getCurrentAccessToken(),
                                    new GraphRequest.GraphJSONArrayCallback() {
                                        @Override
                                        public void onCompleted(
                                                JSONArray jsonArray,
                                                GraphResponse response) {
                                            // Application code for users friends
                                        }
                                    })
                    );
                    batch.addCallback(new GraphRequestBatch.Callback() {
                        @Override
                        public void onBatchCompleted(GraphRequestBatch graphRequests) {
                            // Application code for when the batch finishes
                        }
                    });
                    batch.executeAsync();



                }

                @Override
                public void onCancel() {
                    Toast.makeText(getActivity().getApplicationContext(),"Login Cancel", Toast.LENGTH_LONG).show();

                }

                @Override
                public void onError(FacebookException e) {
                    Toast.makeText(getActivity().getApplicationContext(),"Login Error", Toast.LENGTH_LONG).show();

                }

            });

        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            callbackManager.onActivityResult(requestCode, resultCode, data);

                Intent secondActivityIntent = new          Intent(MainFragment.this.getActivity(), BackCamera.class);

                secondActivityIntent.putExtra("friendlist", friendlist);
                Log.d("friendlistnew", String.valueOf(friendlist));

                startActivity(secondActivityIntent);
    }
            @Override
        public void onStop() {
            super.onStop();

        }

        @Override
        public void onResume() {
            super.onResume();
            Profile profile = Profile.getCurrentProfile();

        }
    }


提前致谢!

最佳答案

可能有多种解决方案,
1.由于您已声明好友列表为静态,因此您可以从任何地方直接访问它


只需将好友列表放在应用程序类中,然后从那里获取或设置数据


3,创建实现可序列化或可仿造的模式类

07-24 19:21