简体   繁体   English

ListView notifyDatasetChanged不刷新视图

[英]ListView notifyDatasetChanged does not refresh view

In my application, I tried to update the listview by 在我的应用程序中,我尝试通过以下方式更新listview

adater.notifyDataSetChanged();

But it does not work. 但这行不通。

I know people have met this problem like this: Android ListView not refreshing after notifyDataSetChanged 我知道人们已经遇到过这样的问题: notifyDataSetChanged后,Android ListView无法刷新

But I have tried that method, it does not work for me. 但是我尝试过这种方法,但对我来说不起作用。

This is my core codes(I tried to refresh the view once the drawer is opened): 这是我的核心代码(打开抽屉后,我试图刷新视图):

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);
}

What's the problem? 有什么问题?

SOLVED: 解决了:

Caused from my f**ing stupid getView method of the DrawerAdapter : 由我的DrawerAdapter愚蠢的getView方法引起的:

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;
}

Fix it to: 修正为:

    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;
    }

Issue is with you swapItems ,You really not required to explicitly call the notifyDataSetChanged. 问题在于您swapItems,您实际上不需要显式调用notifyDataSetChanged。 if your using ArrayAdapter. 如果您使用ArrayAdapter。

Since when you add an item to ArrayAdapter , the ListView will be notified on the changes. 因为当您向ArrayAdapter添加项目时,ListView会收到有关更改的通知。 So you have to add the item to the adapter. 因此,您必须将项目添加到适配器。

Remove this line and the swapItems() method in the DrawerAdapter. 删除此行和DrawerAdapter中的swapItems()方法。

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

When you need to refresh with new item, clear the existing item in the adapter and add new items from onDrawerOpened() 当需要刷新新项目时,请清除适配器中的现有项目,然后从onDrawerOpened()添加新项目。

So, your onDrawerOpened() need to be changed as below 因此,您的onDrawerOpened()需要进行如下更改

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

Thats all.. Hope it works for you 仅此而已..希望它对您有用

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

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