【发布时间】:2011-05-12 12:08:57
【问题描述】:
我正在努力通过NotfiyDataSetChanged 更新我的ExpandableListView。
我正在从网络服务器下载一些数据,这些数据在我的ExpandableListView 中正确显示。
通过单击子项,用户会看到一个对话框,他可以在其中更改一些值。
这个值应该出现在ExpandableListView 中,所以我更改了我班级的背景数据并调用NotifyDataSetChanged()。这对子项目很好,直到我向 Web 服务器发送一个新请求。然后,在我的下载功能和本地更改功能中,NotifyDataSetChanged() 都不起作用。
因为我也想更改一些组项目内容(在对话框之后),所以我也更改了我的组的背景数据。然而,虽然NotifyDataSetChanged() 至少对子项起作用了几次,但它对组项根本没有任何影响。
如果我的后台数据确实发生了变化,我已经检查了调试器和 log.d(),但确实发生了变化。
查看this question 和this 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,那么一切都会正常工作。但是,我确信这不是解决方案。使用适配器有什么意义?
【问题讨论】:
标签: java android user-interface expandablelistview expandablelistadapter