gpt4 book ai didi

android - 为什么我不能在 FacebookCallback 之外使用我的 ArrayList 变量

转载 作者:行者123 更新时间:2023-11-29 20:14:49 27 4
gpt4 key购买 nike

您好,我正在尝试实现 Facebook 登录和获取好友列表。我正在将我 friend 的姓名和个人资料图片网址存储到 ArrayList<TaggedFriends> 中TaggedFriends 类将在何处设置和获取方法。

我现在在名为 MainFragment 的 Fragment 类中工作。在这里,我将值存储到 FacebookCallback session 中的 taggableFriends 变量中。但我无法访问它到任何其他地方我试图将它存储到 SharedPreferences 中,但是当我尝试存储时,它返回空值。

这是我的MainFragment的完整代码

public class MainFragment extends Fragment {
String email, tag = "MainFragment kbt";
ArrayList<TaggableFriends> taggableFriends = new ArrayList<TaggableFriends>();
ArrayList<TaggableFriends> testing = new ArrayList<>();
MainActivity mainActivity ;

private CallbackManager callbackManager;
private TextView textView;

private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;


private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/taggable_friends",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
// handle the result
Log.d("RESPONSE KBT", response.getRawResponse());
try {

JSONObject obj = new JSONObject(response.getRawResponse());
JSONArray array = obj.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject indi = array.getJSONObject(i);
JSONObject pic = indi.getJSONObject("picture");
JSONObject data = pic.getJSONObject("data");
String name = indi.getString("name");
String url = data.getString("url");
TaggableFriends tf = new TaggableFriends(name, url);
taggableFriends.add(tf);
Log.d("NAME", tf.getName());
Log.d("SIZE", ":"+taggableFriends.size());


}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(tag,"SECOND SIZE"+taggableFriends.size());
//
}
}
).executeAsync();

JSONArray mJSONArray = new JSONArray(taggableFriends);
SharedPreferences mPrefs = getActivity().getSharedPreferences("My_File",Context.MODE_PRIVATE);
SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.putString("myKey", mJSONArray.toString());
Log.d(tag,"STRING JSON : "+mJSONArray.toString());
mEditor.commit();
displayMessage(profile);
}

@Override
public void onCancel() {

}

@Override
public void onError(FacebookException e) {

}
};


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

callbackManager = CallbackManager.Factory.create();

accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {

}
};

profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
displayMessage(newProfile);
}
};

accessTokenTracker.startTracking();
profileTracker.startTracking();
}

@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);
textView = (TextView) view.findViewById(R.id.textView);

// loginButton.setReadPermissions("user_friends");
loginButton.setReadPermissions(Arrays.asList("email", "user_friends", "read_custom_friendlists"));
// loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, callback);
Log.d(tag,"taggable "+taggableFriends.size());
// if(taggableFriends.size()!=0)


}

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

}

private void displayMessage(Profile profile) {
if (profile != null) {
textView.setText(profile.getName());

}
}
}

请注意 FacebookCallBack --> onSuccess() 方法。我在那里存储变量 taggedFriends

的值

紧接着我将 taggedFriends 存储到 SharedPreferences 中。但是在那个地方,taggedFriends 变量为空。它不包含任何内容。我检查了 Logcat。

在 onSuccess() 方法内部,它的值在那里,当我使用该方法时,它为空。有什么问题?

最佳答案

图形 API 调用是异步执行的,即在单独的线程中执行。看起来当您尝试将值存储在 SharedPreferences 中时,异步调用尚未完成并且您得到 null。尝试将存储在 SharedPreferences 部分移动到 onCompleted 方法中,在将它添加到 ArrayList 之后立即进行。

关于android - 为什么我不能在 FacebookCallback 之外使用我的 ArrayList 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34132418/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com