尽管调用了notifyDataSetChanged(),但不会调用BaseAdapter,getView

时间:2014-01-02 06:06:04

标签: android baseadapter

虽然每次我更新数据时,都调用了notifyDataSetChanged(),但从未调用函数getView()。 这是我的班级,任何提示?感谢

public class TripGroupAdapter extends BaseAdapter {
    private static final String TAG = LogUtils.makeTag(TripGroupAdapter.class);

    private Context mContext;

    private List<TripGroup> mGroups;

    private Query mQuery;



    public TripGroupAdapter(final Context context, final List<TripGroup> groups, Query query) {
        mContext = context;
        mGroups = groups;
        mQuery = query;
    }

    public void setGroups(final List<TripGroup> groups) {
        mGroups = groups;
        this.notifyDataSetChanged();
    }


    public void addGroup(TripGroup group) {
        if(mGroups == null)
            mGroups = new LinkedList<TripGroup>();

        mGroups.add(group);
        this.notifyDataSetChanged();
    }


    @Override
    public boolean hasStableIds() {
        return true;
    }

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

    @Override
    public TripGroup getItem(final int position) {
        if(getCount() == 0 || position < 0 || position >= getCount())
            return null;
        else
            return mGroups.get(position);
    }

    @Override
    public long getItemId(final int position) {
        TripGroup group = getItem(position);

        if(group == null)
            return -1;
        else
            return group.getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = new TripListItemView(mContext);
        }

        TripListItemView tripView = (TripListItemView) convertView;
        tripView.setBackgroundResource(R.drawable.selector_trip_result_card);
        tripView.set(mGroups.get(position), mQuery, mCache);

        return tripView;
    }

    public void setQuery(final Query query) {
        mQuery = query;
        notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetChanged() {

        super.notifyDataSetChanged();
    }


}

2 个答案:

答案 0 :(得分:5)

我想出了原因,在这里发布了那些有同样问题的人。我使用Otto总线进行广播,捕获广播并更新基础数据setGroups 的功能未在uiThread上运行。所以在那个函数中,我不得不强制它,就像这样:

getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mTripGroupAdapter.setGroups(tripGroupsList);
            }
        });

答案 1 :(得分:0)

在这个问题中并非如此,但getView之后未调用notifyDataSetChanged的另一种可能性,请确保BaseAdapter方法

registerDataSetObserver(DataSetObserver observer) 
unregisterDataSetObserver(DataSetObserver observer) 

没有被覆盖。