java - Android:如何正确使用 NotifyDataSetChanged 和 SimpleExpandableListAdapter?

标签 java android user-interface expandablelistview expandablelistadapter

我正在努力通过 NotfiyDataSetChanged 更新我的 ExpandableListView

我正在从网络服务器下载一些数据,这些数据在我的 ExpandableListView 中正确显示。

通过单击子项目,用户会看到一个对话框,他可以在其中更改某些值。 该值应该显示在 ExpandableListView 中,因此我更改了类中的背景数据并调用了 NotifyDataSetChanged()。这对子项工作正常,直到我向网络服务器发送新请求。然后,在我的下载功能和本地更改功能中,NotifyDataSetChanged() 都不起作用。

因为我也想更改一些组项目内容(在对话框之后),所以我也更改了组的背景数据。但是,虽然 NotifyDataSetChanged() 至少对子项起作用了几次,但它对组项根本没有任何影响。

我已经用调试器和 log.d() 检查了我的背景数据是否真的发生了变化,但确实发生了变化。

调查this questionthis thread ,我相信我正在使用 notifyData... 错误。但我无法弄清楚它应该如何工作。我发现的唯一有用的函数是 registerDataSetObserver(observer),但我也不确定如何使用它。 :/

这是我的代码。可能会有一些小错误,因为我试图将其分解为必要的部分。

@SuppressWarnings("rawtypes")
private List children;
@SuppressWarnings("rawtypes")
private List groups;
private SimpleExpandableListAdapter expListAdapter;


/*download from webserver*/
void function1()
{
    groups = new ArrayList();
    children = new ArrayList();

    */go through a list I downloaded*/
    for (...)
    {
        HashMap groupMap = new HashMap();
        groupMap.put(groupName, downloadedList.getName());
        groupMap.put(groupRate, downloadedList.getMatchrate() + getString(R.string.matchrate));
        groups.add(groupMap);

        ArrayList childList = new ArrayList();
        HashMap childMap;

        */go through a sublist and fill child Map accordingly*/
        for (...)
        {
            childMap = new HashMap();
            childMap.put(...);
            childList.add(childMap);
        }

        children.add(childList);

    }

    if (expListAdapter == null)
    {
        expListAdapter = new SimpleExpandableListAdapter(KnBDiagMan.this, groups, 
                R.layout.xlist_group_item, 
                new String[]
                { groupName, groupRate }, //private final static String components of my activity
                new int[]
                { R.id.title, R.id.rate }, 
                children, 
                R.layout.xlist_child_item, 
                new String[]
                { childName, childValue }, 
                new int[]
                { R.id.child_title, R.id.child_value } 
        )
        {
            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
            {
                /*change of the text color of R.id.child_value's depending on content*/
            }
        };

        setListAdapter(expListAdapter);
    }
    else
    {
        expListAdapter.notifyDataSetChanged();
    }
}

/*change after Dialog*/
void function2()
{
    String savedChildName = (String) ((HashMap) ((ArrayList) children.get(groupPos)).get(childPos)).get(childName);

    for (int i = 0; i < children.size(); ++i)
    {
        List secondLevel = (ArrayList) children.get(i);

        for (int j = 0; j < (secondLevel.size()); ++j)
        {
            HashMap map = (HashMap) secondLevel.get(j);
            String compareName = (String) map.get(childName);

            if (compareName.contentEquals(savedChildName))
            {
                HashMap newMap = new HashMap();
                newMap.put(childName, savedChildName);
                newMap.put(childValue, newValue);
                secondLevel.set(j, newMap);
            }
        }
    }

    List newGroups = new ArrayList();

    for(int i = 0; i < groups.size(); ++i)
    {
        String savedGroupName = (String) ((HashMap) groups.get(i)).get(groupName);
        HashMap groupContent = new HashMap();
        groupContent.put(groupName, savedGroupName);
        groupContent.put(groupRate, getString(R.string.newString));
        newGroups.add(groupContent);
    }

    groups = newGroups;

    expListAdapter.notifyDataSetChanged();
}

更新: 如果我创建一个新的 SimpleExpandableListAdapter 而不是调用 notifyDataSetChanged,一切都会按预期进行。但是,我确信这不是解决方案。使用适配器有什么意义?

最佳答案

您正在用新引用替换您对 SimpleExpandableListAdapter 数据对象的本地引用...而不是:

List newGroups = new ArrayList();
...
groups = newGroups;

尝试:

groups.clear();

// Add items here
...

expListAdapter.notifyDataSetChanged();

关于java - Android:如何正确使用 NotifyDataSetChanged 和 SimpleExpandableListAdapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5977737/

相关文章:

ios - Apple在这里使用什么控件? (包括截图)UIButton? UITableViewCell?还有别的吗?

java - 更改 ListView 的高度会破坏对齐方式

java - 二维不同口味类型数组之间的区别?这两种类型的优点

java - 当我更改 ImageButton 图像时,没有实时更改

android - 回收位图 Ice Cream Sandwich 强制关闭

javascript - 您认为可以将哪些想法应用到这个 GUI 中,使其更适合真人使用?

python - 新的 wxpython 控件在调整大小之前不会显示

java - JSF 与 JavaFX : Pros & Cons

java - 不使用现有算法或排序技术对整数元素进行排序

java - float 操作按钮未显示在 bottomsheetfragment 折叠状态