繁体   English   中英

notifyDataSetChanged()不起作用

[英]notifyDataSetChanged() doesn't work

我昨天遇到了这个问题:这里有一个服务器返回的列表,列表中有一个Hashmap列表(List)和一个Hashmap。我有两种情况,一种是当hashmap列表为null时,我添加了一个项,然后调用notifyDataSetChanged()不起作用,另一种情况是当我更改了hashmap中的值和notifyDataSetChanged()时,它也没有刷新。而且我确定数据已更改成功。

它在A活动中显示,而更改的数据在B活动中。

活动:

protected List<CommentEntity> mDataList;
protected ListView mListView;
protected BaseListAdapter mAdapter;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    int position = data.getIntExtra("position", -1);
    if (position > -1) {
        CommentEntity entity = (CommentEntity) data.getSerializableExtra(AppVars.Keys.EXTRA_ENTITY);
        mDataList.set(position, entity); // I want this list update
        mAdapter.notifyDataSetChanged();
    } else {
        // error
    }
}
public class BaseListAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList == null ? null : mDataList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // actually this adapter is written in MyBaseFragment, and the child
        // Fragment will override setupItemView() method.it's working fine
        return setupItemView(position, convertView, parent);
    }
}

B活动:

private CommentEntity mCommentEntity;
// the list below is inside CommentEntity, QuoteEntity is extends HashMap<>.
private List<QuoteEntity> mQuoteList;
private QuoteAdapter mAdapter;
public void update(QuoteEntity newQuote) {
    mQuoteList.add(newQuote);
    mCommentEntity.setReplyRows(mQuoteList);
    mAdapter.notifyDataSetChanged();// This adapter is just working fine.
    setResult(-1, getIntent().putExtra(AppVars.Keys.EXTRA_ENTITY, mCommentEntity));
}

尝试重置listview而不是使用notifyDataSetChanged();

           ListView lv = (ListView)FindViewById(R.id.lv);
           BaseListAdapter aa = new BaseListAdapter(//constructor's parameters);
           lv.setAdapter(aa);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM