在我的情況下,我使用片段作爲一個活動中的選項卡。我想在創建片段時加載信息。所以我用一個Runnable運行httprequest
並把這些響應返回給定製的處理,我叫mAdapter.notifyDataSetChanged()
notifyDataSetChanged無法更新我的ListView片段
這裏談到的問題的片段中,該片段內的ListView未能更新其觀點後,我調用的方法notifyDataSetChanged()
。
我跟蹤堆棧,並發現的notifyDataSetChanged()
呼叫後,適配器並調用它的方法getCount()
和getCount()
做了零返回一個值,但適配器的getCount()
通話後沒有調用它的方法getView()
。
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);
}
}
ÿmActivity .mAdapter.notifyDataSetChanged();?你可以簡單地使用adpter.noti ....並且我看不到mAdapter聲明 – KOTIOS
mAdapter的聲明超出了方法onCreate() – luminos