在使用数据作为Map的notifyDataSetChanged之后,Android ListView不会刷新

时间:2014-06-27 13:59:44

标签: android listview baseadapter

我使用带有地图值的列表适配器作为数据。当我使用adapter.notifyDataSetChanged();列表中的数据不更新时。但是,如果我用ArrayList替换Map,一切正常。以下是我的适配器代码。

public class CategoryAdapter extends BaseAdapter {
ArrayList<Category> list = new ArrayList<Category>();
Context context;

public CategoryAdapter(Context context, Map<String, Category> categories) {
    this.context = context;
    list.clear();
    list.addAll(categories.values());
}

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

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.category_list_item, null);
            handler = new ViewHandler();
            handler.name = (TextView) convertView.findViewById(R.id.name);
            handler.count = (TextView) convertView.findViewById(R.id.count);
            convertView.setTag(handler);
        } else {
            handler = (ViewHandler) convertView.getTag();
        }
        Category category = list.get(position);
        handler.name.setText(category.getMenuName());
        handler.count.setText(category.getCount() + "");
        if (category.getCount() <= 0) {
            handler.count.setVisibility(View.INVISIBLE);
        } else {
            handler.count.setVisibility(View.VISIBLE);
        }

        return convertView;
    }
}

我的活动代码是

private void loadCategories() {
    sampleDB = openOrCreateDatabase(AppConstants.DB_NAME, MODE_PRIVATE,
            null);
    Cursor menuCursor = sampleDB.rawQuery("select * from menu", null);

    categories.clear();
    while (menuCursor.moveToNext()) {
        String menu = menuCursor.getString(menuCursor
                .getColumnIndex("name"));
        String id = menuCursor.getString(menuCursor.getColumnIndex("id"));
        Category category = new Category(menu, id);
        categories.put(id, category);
    }
    menuCursor.close();
    categoryAdapter.notifyDataSetChanged();
}

oncreate()方法中我已声明适配器如下

categoryAdapter = new CategoryAdapter(context, categories);
        categoryList.setAdapter(categoryAdapter);
        loadCategories();

每次点击刷新按钮,都会调用loadCategories();方法。 在相同的代码中,如果我用ArrayList替换Map,一切正常。

现在我的问题是为什么List没有使用Map值刷新。请给我澄清一下。

提前致谢。

2 个答案:

答案 0 :(得分:5)

您必须向CategoryAdapter添加方法才能更改实例列表,如此

public class CategoryAdapter extends BaseAdapter {
ArrayList<Category> list = new ArrayList<Category>();
Context context;

public CategoryAdapter(Context context, Map<String, Category> categories) {
    this.context = context;
    list.clear();
    list.addAll(categories.values());
}

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

//ADD THIS METHOD TO CHANGE YOUR LIST
public void addItems(Map<String, Category> categories){
    list.clear();
    list.addAll(categories.values());
}

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.category_list_item, null);
        handler = new ViewHandler();
        handler.name = (TextView) convertView.findViewById(R.id.name);
        handler.count = (TextView) convertView.findViewById(R.id.count);
        convertView.setTag(handler);
    } else {
        handler = (ViewHandler) convertView.getTag();
    }
    Category category = list.get(position);
    handler.name.setText(category.getMenuName());
    handler.count.setText(category.getCount() + "");
    if (category.getCount() <= 0) {
        handler.count.setVisibility(View.INVISIBLE);
    } else {
        handler.count.setVisibility(View.VISIBLE);
    }

    return convertView;
}
}

并像这样更改你的loadCategories(注意我在notifyDataSetChanged()之前调用addItems()

private void loadCategories() {
sampleDB = openOrCreateDatabase(AppConstants.DB_NAME, MODE_PRIVATE,
        null);
Cursor menuCursor = sampleDB.rawQuery("select * from menu", null);

categories.clear();
while (menuCursor.moveToNext()) {
    String menu = menuCursor.getString(menuCursor
            .getColumnIndex("name"));
    String id = menuCursor.getString(menuCursor.getColumnIndex("id"));
    Category category = new Category(menu, id);
    categories.put(id, category);
}
menuCursor.close();

//ADD CALL TO addItems TO UPDATE THE LIST OF THE categoryAdapter instance
categoryAdapter.addItems(categories);
categoryAdapter.notifyDataSetChanged();
}

答案 1 :(得分:2)

你试过说:

categoryList.setAdapter(null);
categoryList.setAdapter(categoryAdapter);