Android的ListView不刷新与数据地图

Android的ListView不刷新与数据地图

本文介绍了notifyDataSetChanged后,Android的ListView不刷新与数据地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的列表适配器映射值数据。当我使用 adapter.notifyDataSetChanged(); 在列表中的数据没有更新。但是,如果我有ArrayList的一切工作正常更换地图。以下是我的适配器code。

I used list adapter with map values as data. When I use adapter.notifyDataSetChanged(); the data in the list not updating. But if I replace the Map with ArrayList everything working fine. Following is my adapter code.

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

和我的活动code是

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()方法我宣布适配器如下:

in oncreate() method I have declared adapter as follows

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

我每次点击刷新按钮调用 loadCategories(); 方法。
在同一code,如果我有ArrayList的一切工作正常更换地图。

Every time I click on refresh button it calls loadCategories(); method.In the same code if I replace Map with ArrayList everything working fine.

现在我的问题是,为什么名单没有与地图值耳目一新。请给我澄清有关本。

Now My question is why List is not refreshing with Map values. Please give me clarification regarding this.

先谢谢了。

推荐答案

您必须添加一个方法来你的 CategoryAdapter 来更改实例的名单,像这样

You must add a method to your CategoryAdapter to change the instance's list, like so

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()()

and change your loadCategories like so (note that I call the addItems() before notifyDataSetChanged()

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

这篇关于notifyDataSetChanged后,Android的ListView不刷新与数据地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:59