gpt4 book ai didi

android - executeAsync Facebook android SDK

转载 作者:行者123 更新时间:2023-11-29 01:35:47 24 4
gpt4 key购买 nike

我正在调用 request.executeAnsyc 以用信息填充我的数组并显示在 ListView 上。遗憾的是,在 request.executeAnsyc 完成之前调用了数组。所以我的 Fragment 的 CreatView 方法上没有显示任何内容。

有没有办法在 request.executeAnsyc 完成后使用数组并在 ListView 上显示信息?!

最好的问候。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.kizombalistfestival,
container, false);
listUsersInformation = new ArrayList<UserInfo>();

// TODO Auto-generated method stub

Session session = Session.getActiveSession();
new Request(session, "/me", null, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {

/* handle the result */

String serverresponse = response
.getGraphObject().getInnerJSONObject()
.toString();
Log.e("serverresponse", "" + serverresponse);
JSONObject jsonobj;

try {

jsonobj = new JSONObject(serverresponse);
UserInfo temp = new UserInfo(jsonobj
.getString("id"), jsonobj
.getString("first_name"), jsonobj
.getString("gender"), jsonobj
.getString("last_name"), jsonobj
.getString("link"), jsonobj
.getString("locale"), jsonobj
.getString("name"), jsonobj
.getString("timezone"), jsonobj
.getString("updated_time"), jsonobj
.getString("verified"));

listUsersInformation.add(temp);




} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}).executeAsync();




list = (ListView) rootView.findViewById(R.id.list_festivals);
list.setAdapter(new ArrayAdapter<UserInfo>(getActivity(),
android.R.layout.simple_list_item_1, listUsersInformation));



return rootView;

}

最佳答案

我可以想到两种方法来实现它。

<强>1。在 finally block 中使用处理程序更新 ListView

new Request(session, "/me", null, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {

/* handle the result */

String serverresponse = response
.getGraphObject().getInnerJSONObject()
.toString();
Log.e("serverresponse", "" + serverresponse);
JSONObject jsonobj;

try {

jsonobj = new JSONObject(serverresponse);
UserInfo temp = new UserInfo(jsonobj
.getString("id"), jsonobj
.getString("first_name"), jsonobj
.getString("gender"), jsonobj
.getString("last_name"), jsonobj
.getString("link"), jsonobj
.getString("locale"), jsonobj
.getString("name"), jsonobj
.getString("timezone"), jsonobj
.getString("updated_time"), jsonobj
.getString("verified"));

listUsersInformation.add(temp);




} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// add your code here
handler.post(new Runnable(){
@Override
public void run(){
list = (ListView)rootView.findViewById(R.id.list_festivals);
list.setAdapter(new ArrayAdapter<UserInfo>(getActivity(),
android.R.layout.simple_list_item_1, listUsersInformation));
}
});
}

}
}).executeAsync();

<强>2。使用广播接收器

首先,您应该在要更新 ListView 的 fragment 中注册接收器。然后,在您的 Facebook 函数类中,将 Broadcast(intent) 发送到您注册的 fragment ,如下所示:

// your fb request callback
try {

jsonobj = new JSONObject(serverresponse);
Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
intent.putExtra("id", id);
intent.putExtra("first_name", first_name);
intent.putExtra("gender", gender);
intent.putExtra("last_name", last_name);
// ... and so on
context.sendBroadcast(intent);
}

最终,在您注册了 broadcastreceiver 的 fragment 中,

private final BroadcastReceiver streamReceiver = new    BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){

///Waking up mobile of it is sleeping
WakeLocker.acquire(getActivity().getApplicationContext());
String id = intent.getExtras().getString("id");
String first_name = intent.getExtras().getString("first_name");
String gender = intent.getExtras().getInt("gender");
...
...
UserInfo temp = new UserInfo(id,
first_name,
gender, .....);

listUsersInformation.add(temp);

yourAdapter.notifyDataSetChanged();


WakeLocker.release();
}
};

关于android - executeAsync Facebook android SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28165871/

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