Android: How to correctly use NotifyDataSetChanged with SimpleExpandableListAdapter?
I am struggeling updating my ExpandableListView
via NotfiyDataSetChanged
.
I am downloading some data from a webserver, which is displayed correctly in my ExpandableListView
.
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.
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.
I have checked with debugger & log.d() if my background data really changes, but it does.
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. :/
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[]
开发者_如何学Go { 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();
}
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?
You're replacing your local reference to the SimpleExpandableListAdapter's data objects with a new reference... Instead of:
List newGroups = new ArrayList();
...
groups = newGroups;
Try:
groups.clear();
// Add items here
...
expListAdapter.notifyDataSetChanged();
精彩评论