ListView notifyDatasetChanged不刷新视图

时间:2014-04-10 01:39:25

标签: android listview

在我的应用程序中,我尝试通过

更新列表视图
adater.notifyDataSetChanged();

但它不起作用。

我知道有人遇到过这样的问题:Android ListView not refreshing after notifyDataSetChanged

但是我尝试过这种方法,它对我不起作用。

这是我的核心代码(我打开抽屉后尝试刷新视图):

public class DrawerItem {
    private String name;
    private int icon = -1;
    private String counter = "0";
    private boolean showCounter;
    private ItemType itemType;

    public DrawerItem(String name, int icon, ItemType itemType, boolean showCounter) {
        this.name = name;
        this.icon = icon;
        this.showCounter = showCounter;
        this.itemType = itemType;
    }
    //getter and setter omitted
    public enum ItemType {
        Action, Layer, Title
    }
}


private List<DrawerItem> getDrawerItems() {
    ArrayList<DrawerItem> items = new ArrayList<DrawerItem>();

    DrawerItem layer = new DrawerItem("Layer", -1, DrawerItem.ItemType.Title, false);
    DrawerItem layer01 = new DrawerItem("Layer01", R.drawable.drawer_ic1, DrawerItem.ItemType.Action, false);
    DrawerItem layer02 = new DrawerItem("Layer02", R.drawable.drawer_ic2, DrawerItem.ItemType.Action, true);
    DrawerItem layer03 = new DrawerItem("Layer03", R.drawable.drawer_ic3, DrawerItem.ItemType.Action, true);
    DrawerItem layer04 = new DrawerItem("Layer04", R.drawable.drawer_ic4, DrawerItem.ItemType.Action, false);


    items.add(layer);
    items.add(layer01);
    items.add(layer02);
    items.add(layer03);
    items.add(layer04);
    if (mLayerPoint2 != null) {
        layer02.setCounter(String.valueOf(mLayerPoint2.getFeatureCounter()));
    }

    if (mLayerPoint3 != null) {
        layer03.setCounter(String.valueOf(mLayerPoint3.getFeatureCounter()));
    }
    return items;
}

private class DrawerAdapter extends ArrayAdapter<DrawerItem> {
    private List<DrawerItem> mData = new ArrayList<DrawerItem>();
    public DrawerAdapter(Context context, int resource) {
        super(context, resource);
    }
    public void swapItems(List<DrawerItem> items) {
        this.mData.clear();
        this.mData.addAll(items);
        this.notifyDataSetChanged();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DrawerItem item = getItem(position);

        if (convertView == null) {
            switch (item.getItemType()) {
                case Action:
                    convertView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.drawer_item_action, null);
                    .....
                    break;
                case Title:
                    convertView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.drawer_item_title, null);
                    ...
                    break;
            }
        }
        return convertView;
    }
}


public void onCreate(...){
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerListAdapter = new DrawerAdapter(this, R.layout.drawer_item_action);
    List<DrawerItem> items = getDrawerItems();
    mDrawerListAdapter.addAll(items);
    mDrawerList.setAdapter(mDrawerListAdapter);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {
        public void onDrawerClosed(View view) {
        }

        public void onDrawerOpened(View drawerView) {
            mDrawerListAdapter.swapItems(getDrawerItems());
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

有什么问题?

2 个答案:

答案 0 :(得分:1)

解决:

getView的{​​{1}}方法引起的DrawerAdapter方法引起:

public View getView(int position, View convertView, ViewGroup parent) {
    DrawerItem item = getItem(position);

    if (convertView == null) {
        switch (item.getItemType()) {
            case Action:
                convertView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.drawer_item_action, null);
                .....
                break;
            case Title:
                convertView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.drawer_item_title, null);
                ...
                break;
        }
    }
    return convertView;
}

将其修复为:

    switch (item.getItemType()) {
        case Action:
            if (convertView == null) {
                convertView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.drawer_item_action, null);
            }
            ....
            break;
        case Title:
            if (convertView == null) {
                convertView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.drawer_item_title, null);
            }
            ....
            break;
    }

答案 1 :(得分:0)

问题在于你的swapItems,你真的不需要显式调用notifyDataSetChanged。 如果您使用ArrayAdapter。

因为当您向ArrayAdapter添加项目时,ListView将会收到有关更改的通知。 所以你必须将项目添加到适配器。

在DrawerAdapter中删除此行和swapItems()方法。

private List<DrawerItem> mData = new ArrayList<DrawerItem>();

当您需要刷新新项目时,清除适配器中的现有项目并从onDrawerOpened()添加新项目

所以,您的onDrawerOpened()需要更改如下

 public void onDrawerOpened(View drawerView) {
        // clear the existing items
        mDrawerListAdapter.clear();
        // add the new item
        mDrawerListAdapter.addAll(getDrawerItems())
    }

多数人..希望它适合你