notifyDataSetChanged无法更新片段中的ListView

时间:2014-06-10 04:34:10

标签: android listview android-fragments

就我而言,我使用片段作为活动中的标签。我想在创建片段时加载信息。所以我使用Runnable来运行httprequest并将这些响应放回到我称之为mAdapter.notifyDataSetChanged()的片段内的自定义处理程序

出现问题,在调用方法notifyDataSetChanged()后,片段内的listView无法更新其视图。

我跟踪了堆栈并发现在调用notifyDataSetChanged()之后,适配器确实调用了它的方法getCount()并且getCount()确实返回了一个超过零的值,但是适配器没有在调用getView()之后调用其方法getCount()

public class ProductReviewFragment extends Fragment {


private View mBackgroundView = null;

private ProductHelper mProductHelper = null;

private PullToRefreshListView mListView = null;

private int pre = 20;

private long mark = 0;

private long productId = 0;

private List<ProductThread> listProductThread = new ArrayList<ListProductReviewResponse.ProductThread>();

private class MyHandler extends Handler{
    private ProductReviewFragment mActivity = null;

    public MyHandler(ProductReviewFragment activity){
        mActivity = activity;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        switch (msg.what) {
        case ProductHelper.GET_PRODUCT_REVIEW_SUCCESS:
            ListProductReviewResponse response = (ListProductReviewResponse)msg.obj;
            if (response != null) {
                if (response.getProductThreadList()!=null) {
                    mActivity.listProductThread.addAll(response.getProductThreadList());
                }
                mActivity.mark = response.getMark();
                mActivity.mAdapter.notifyDataSetChanged();
            }
            mActivity.mListView.onRefreshComplete();
            if (mActivity.listProductThread.size()>=response.getTotalNumber()) {
                mActivity.mListView.setMode(Mode.DISABLED);
            }
            break;
        case ProductHelper.GET_PRODUCT_REVIEW_FAILED:
            mActivity.mListView.onRefreshComplete();
            if (msg.obj!=null) {
                NetWorkException exception = (NetWorkException)msg.obj;
                NetWorkException.handlerResultNetWorkNotAvailable(mActivity.getActivity(), exception);
            }
        default:

            break;
        }

    }
}

private MyHandler mHandler = new MyHandler(this);

private class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        int i = listProductThread.size();
        return i;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null) {
            holder = new ViewHolder();

            convertView = View.inflate(getActivity(), R.layout.product_review_cell, null);

            holder.rivHead = (RoundImageView)convertView.findViewById(R.id.product_comment_head);

            holder.tvName = (TextView)convertView.findViewById(R.id.product_comment_user);

            holder.tvContent = (TextView)convertView.findViewById(R.id.product_comment_content);

            convertView.setTag(holder);
        }
        holder = (ViewHolder)convertView.getTag();

        holder.rivHead.setImageInfo(ImageInfo.obtain(ImageCategories.CATEGORY_ROUND_HEAD, listProductThread.get(position).getUser().getAvatarURL()));

        holder.tvName.setText(listProductThread.get(position).getUser().getUserName());

        holder.tvContent.setText(listProductThread.get(position).getContent());

        return convertView;
    }

    private class ViewHolder {
        RoundImageView rivHead = null;
        TextView tvName = null;
        TextView tvContent = null;
    }
}

private MyAdapter mAdapter = new MyAdapter();

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

    Bundle bundle = getArguments();
    if (bundle!=null) {
        productId = bundle.getLong(ProductHelper.PRODUCT_ID);
    }

    mProductHelper = new ProductHelper(getActivity().getApplicationContext());
    mProductHelper.getProductReview(productId, mark, pre, mHandler);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mBackgroundView = inflater.inflate(R.layout.pulltorefresh_list_fragment, null);
    return mBackgroundView;
}

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

    initUI();

}

public void initUI(){


    mListView = (PullToRefreshListView) mBackgroundView.findViewById(R.id.fragment_listview);

    mListView.setMode(Mode.PULL_FROM_END);

    mListView.setAdapter(mAdapter);
}

}

1 个答案:

答案 0 :(得分:0)

你已经将你的oncreate代码放在onCreateView方法或ActivityCreated方法上了。因为你的Xml UI绑定是在onCreateview方法和onCreate方法之前调用onCreateView所以只需移动你的代码......