Android:如何正确使用NotifyDataSetChanged与SimpleExpandableListAdapter?

jel*_*ish 2 java user-interface android expandablelistview expandablelistadapter

我正在努力更新我的ExpandableListView通道NotfiyDataSetChanged.

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

通过单击子项,用户将显示一个对话框,可以在其中更改某个值.这个值应该显示在ExpandableListView,所以我改变了我的类中的背景数据并调用NotifyDataSetChanged().这适用于子项,直到我向Web服务器发送新请求.然后,无论是在我的下载功能还是我的本地更改功能,都NotifyDataSetChanged()可以.

由于我想要更改某些组项内容(在对话框之后),我也会更改我的组的背景数据.但是,虽然NotifyDataSetChanged()对于子项目至少工作几次,但它对组项目完全没有任何影响.

如果我的后台数据确实发生变化,我已经使用debugger&log.d()进行了检查,但确实如此.

看看这个问题这个帖子,我相信我正在使用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();
}
Run Code Online (Sandbox Code Playgroud)

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

Aus*_*son 6

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

List newGroups = new ArrayList();
...
groups = newGroups;
Run Code Online (Sandbox Code Playgroud)

尝试:

groups.clear();

// Add items here
...

expListAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)