Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Having trouble with similar task. ExpandableListview placed in Fragment with custom group/item views. My solution is use SharedPreferences based on <a href="https://stackoverflow.com/a/4186345/2320413">this answer</a>. It's may be not optimal (I learn Java/Android less than a month) but only one that worked. Storing/restoring via Bundle does not work for me :(</p> <ol> <li><p>Write 2 methods for collecting and setting expanded states:</p> <pre><code>private ArrayList&lt;Long&gt; getExpandedIds() { // private ExpandableListView lv defined above BaseExpandableListAdapter adp = (BaseExpandableList) lv.getExpandableListAdapter(); if (adp != null) { int sz = adp.getGroupCount(); ArrayList&lt;Long&gt; ids = new ArrayList&lt;Long&gt;(); for (int i=0; i&lt;sz; i++) if lv.isGroupExpanded(i)) ids.add(adp.getGroupId(i)); return ids; } else { return null; } } private void setExpandedIds(ArrayList&lt;Long&gt; ids) { if ((ids == null) || (ids.size()==0)) return; BaseExpandableListAdapter adp = (BaseExpandableListAdapter) lv.getExpandableListAdapter(); if (adp != null) { int sz = adp.getGroupCount(); for (int i=0;i&lt;sz;i++) { long id = adp.getGroupId(i); for (long n : ids) { if (n == id) { lv.expandGroup(i); break; } } } } } </code></pre></li> <li><p>Use above methods for save and restore state in SharedPreferences:</p> <pre><code>@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); ArrayList&lt;Long&gt; expIds = getExpandedIds(); if (expIds != null) { try { @SuppressWarnings("ConstantConditions") SharedPreferences pref = this.getActivity().getPreferences(Activity.MODE_PRIVATE); SharedPreferences.Editor ed = pref.edit(); String s = TextUtils.join(";", expIds.toArray()); ed.putString(LV_NAME, s); ed.commit(); } catch (NullPointerException e) { Log.e("YAMK", "Store prefs failed."); } } } // in onCreateView: try { @SuppressWarnings("ConstantConditions") SharedPreferences pref = getActivity().getPreferences(Activity.MODE_PRIVATE); ArrayList&lt;Long&gt;expIds = new ArrayList&lt;Long&gt;(); for (String s : TextUtils.split(pref.getString(LV_NAME, ""), ";")) { expIds.add(Long.parseLong(s)); } } catch (NullPointerException e) { // } setExpandedIds(expIds); </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload