notifyDataSetChanged()不刷新ListView

时间:2017-03-18 15:56:04

标签: java android listview baseadapter

我有以下问题:

我正在尝试从firebase加载一些数据,在加载数据并将其存储在HashMap中后,我调用“updateAdapter”函数(在下面的适配器中声明),虽然数据已被更改,但ListView不会刷新直到一些后来的随机(IMO)时刻。我阅读了几个与我的问题相关的帖子,并尝试了不同的解决方案,但他们并没有解决我的问题。顺便说一句 - 是的,我检查了适配器是否丢失了对列表的引用而它没有。

我的适配器看起来像这样:

public class NoteAdapterHashMap extends BaseAdapter{
    private ArrayList mData = new ArrayList();

    public NoteAdapterHashMap(Map<String, Note> map) {
        mData.addAll(map.entrySet());

    }

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

    @Override
    public Map.Entry<String, Note> getItem(int position) {
        return (Map.Entry) mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO implement logic with ID
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final View result;

        if (convertView == null) {
            result = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note_listview, parent, false);
        } else {
            result = convertView;
        }

        Note note = getItem(position).getValue();
        if(note!=null){
            TextView title = (TextView) result.findViewById(R.id.list_note_title);
            TextView date = (TextView) result.findViewById(R.id.list_note_date);
            TextView content = (TextView) result.findViewById(R.id.list_note_content);
            title.setText(note.getmTitle());
            date.setText(note.getDateFormatted(parent.getContext()));
            content.setText(note.getmContent());

            if(note.getmContent().length()>50){
                content.setText(note.getmContent().substring(0,50));
            }

        }


        return result;
    }
    public void updateAdapter(HashMap<String,Note> map){
        this.mData.clear();
        this.mData.addAll(map.entrySet());
        this.notifyDataSetChanged();
    }
    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        super.registerDataSetObserver(observer);
    }
}

提前致谢!

0 个答案:

没有答案
相关问题