0

我对片段上的 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?

提前致谢

4

1 回答 1

0

这是 ItemAdapter 类

class ItemAdapter extends BaseAdapter {

    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

    private class ViewHolder {
        public TextView text;
        public ImageView image;
    }

    @Override
    public int getCount() {
        return friends.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;

        if (convertView == null) {
            view = getActivity().getLayoutInflater().inflate(
                    R.layout.friend_list_item, parent, false);
            holder = new ViewHolder();
            holder.text = (TextView) view.findViewById(R.id.tvFriendName);
            holder.image = (ImageView) view
                    .findViewById(R.id.imgFriendPhoto);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        Friend friend = friends.get(position);

        if (friend != null) {
            String name = friend.getName();
            String photoUrl = friend.getPhoto();

            holder.text.setText(name);

            imageLoader.displayImage(photoUrl, holder.image, imageOptions,
                    animateFirstListener);
        }

        return view;
    }
}
于 2013-06-01T00:03:28.860 回答