Android:SimpleExPandableListAdapterを使用してNotifyDataSetCangedを正しく使用する方法?
-
12-11-2019 - |
質問
ExpandableListView
を介してNotfiyDataSetChanged
を更新します。
私のExpandableListView
に正しく表示されるWebサーバーからデータをダウンロードしています。
子アイテムをクリックすると、ユーザーは自分が変更できるダイアログを表示します。
この値は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();
}
.
更新:
SimpleExpandableListAdapter
を呼び出す代わりに、新しいnotifyDataSetChanged
を作成した場合、すべてが目的としています。しかし、私はこれが解決策になることができないと確信しています。アダプターをまったく使用することのあることは何でしょうか?
解決
SimpleExPandableListadapterのSimpleExPandableListAdapterのデータオブジェクトを新しい参照...ではなく、次のように置き換えています。
List newGroups = new ArrayList();
...
groups = newGroups;
.
試してみてください:
groups.clear();
// Add items here
...
expListAdapter.notifyDataSetChanged();
.