我对片段上的 ListView 有疑问(该片段扩展了 SherlockFragment)。我想显示 Facebook 朋友的列表(我在 OnCreateView 方法上使用 executeBatchAsync,并为 ListView 使用自定义适配器。此适配器扩展了 BaseAdapter)。我能够用结果填充一个数组,但问题是片段总是首先呈现,所以它显示并清空 ListView。这是代码的一部分。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group,
Bundle saved) {
View root = inflater.inflate(R.layout.fragment_guests, group, false);
final Session session = Session.getActiveSession();
if (session.isOpened()) {
pd = ProgressDialog.show(getActivity(),
getString(R.string.loading),
getString(R.string.loading_friend_list));
pd.setCancelable(false);
Bundle params = new Bundle();
String fqlQuery = "SELECT uid, name, pic_square FROM user WHERE uid IN "
+ "(SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY name";
params.clear();
params.putString("q", fqlQuery);
fbRequest = new Request(session, "fql", params, HttpMethod.GET,
new Request.Callback() {
@Override
public void onCompleted(Response response) {
try {
GraphObject graphObject = response
.getGraphObject();
JSONObject jsonObject = graphObject
.getInnerJSONObject();
JSONArray jsonArray = jsonObject
.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject friend = jsonArray
.getJSONObject(i);
Friend f = new Friend();
f.setUid(friend.getString("uid"));
f.setName(friend.getString("name"));
f.setPhoto(friend.getString("pic_square"));
friends.add(f);
}
list = (ListView) getView().findViewById(
R.id.friendsList);
list.setAdapter(adapter);
pd.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Request.executeBatchAsync(fbRequest);
}
return root;
}
所以,我的问题是:如何正确显示 ListView?
提前致谢