i got this problem yesterday: Here I have a list returned by server, and there is a Hashmap list(List) and a Hashmap inside the list.And I got two situations, one is when the hashmap list is null, I add a new item into it,and then call notifyDataSetChanged() doesn't work, and the other situation is when i changed the value in hashmap, and notifyDataSetChanged(), it doesn't refresh either.And I'm sure the data has been changed successfully.
it shows in A activity and changed data in B activity.
A activity:
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 activity:
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));
}
Try resetting listview instead of using notifyDataSetChanged();
ListView lv = (ListView)FindViewById(R.id.lv);
BaseListAdapter aa = new BaseListAdapter(//constructor's parameters);
lv.setAdapter(aa);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.