问题描述
我struggeling更新我的 ExpandableListView
通过 NotfiyDataSetChanged
。
I am struggeling updating my ExpandableListView
via NotfiyDataSetChanged
.
我是从一个网络服务器,这是在正确显示下载一些数据,我的 ExpandableListView
。
I am downloading some data from a webserver, which is displayed correctly in my ExpandableListView
.
通过点击一个子项,用户被显示一个对话框,在那里他可以改变一些价值。
这个值应该在 ExpandableListView
,所以我改变背景数据在我的类并调用 NotifyDataSetChanged()
。这工作正常的子项,直到我打发到Web服务器的新请求。于是,无论是在我的下载功能,也没有我的地方改变功能, NotifyDataSetChanged()
和
By clicking on a child item, the user gets displayed a dialog where he can change some value.This value is supposed to show up in the ExpandableListView
, so I change the background data in my class and call NotifyDataSetChanged()
. This works fine for the child items, until I send off a new request to the Webserver. Then, neither in my downloading function nor my local change function, NotifyDataSetChanged()
works.
因为我想改变一些组项目的内容,以及(对话)后,我也改变我集团的背景资料了。然而,虽然 NotifyDataSetChanged()
工作至少几十倍的子项,它不会显示在所有的项目组有任何影响。
As I want to change some group item content as well (after the dialog), I also change the background data of my groups, too. However, while NotifyDataSetChanged()
works at least a few times for the child items, it doesn't at all show any effect on the group items.
我与调试器和放大器检查; log.d()如果我的背景数据真正改变,但它确实。
I have checked with debugger & log.d() if my background data really changes, but it does.
展望并的,我相信我使用 notifyData code> ...错误。但我无法弄清楚它是如何工作的。只有我发现有用的功能是
registerDataSetObserver(观察员)
,但我不知道如何使用这一点。 :/
Looking into this question and this thread, I believe I am using notifyData
... wrong. But I can't figure out how it's supposed to work. Only useful function I found was registerDataSetObserver(observer)
, but I wasn't sure how to use this as well. :/
下面是我的code。可能有一些小的错误,我试图剥离下来的必要部分。
Here is my code. There might be some small errors, as I tried to strip it down to the necessary parts.
@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();
}
更新:
一切正常,如果我创建调用的 notifyDataSetChanged
新的 SimpleExpandableListAdapter
不是作为intented。不过,我相信这绝不是解决方案。会是什么点使用适配器呢?
Update:Everything is working as intented if I create a new SimpleExpandableListAdapter
instead of calling notifyDataSetChanged
. However, I am sure this can't be the solution. What would be the point in using an Adapter at all?
推荐答案
您要更换你的本地引用一个新的参考SimpleExpandableListAdapter的数据对象......相反的:
You're replacing your local reference to the SimpleExpandableListAdapter's data objects with a new reference... Instead of:
List newGroups = new ArrayList();
...
groups = newGroups;
尝试:
groups.clear();
// Add items here
...
expListAdapter.notifyDataSetChanged();
这篇关于Android的:如何正确使用NotifyDataSetChanged与SimpleExpandableListAdapter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!