2010-11-15 15 views
11

की विस्तारित/ध्वस्त स्थिति को सहेजें और पुनर्स्थापित करें मेरे पास एक विस्तारणीय लिस्टएक्टिविटी है (एक SimpleCursorTreeAdapter का उपयोग करके) जो उपयोगकर्ता द्वारा बच्चे-तत्व पर क्लिक करते समय एक और गतिविधि शुरू करता है। नई गतिविधि में बैक-बटन दबाते समय सभी सूची-आइटम फिर से ध्वस्त हो जाते हैं। मैं ExpandableListActivity की विस्तारित स्थिति को कैसे सहेजूं और इसे दोबारा बहाल करूं।एक विस्तारणीय लिस्टएक्टिविटी

मैं पहले से ही लागू किया onSaveInstanceState() और onRestoreInstanceState() इस तरह की कोशिश की ...

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    Parcelable listState = getExpandableListView().onSaveInstanceState(); 
    outState.putParcelable("ListState", listState); 
} 


@Override 
protected void onRestoreInstanceState(Bundle state) { 
    super.onRestoreInstanceState(state); 
    Parcelable listState = state.getParcelable("ListState"); 
    getExpandableListView().onRestoreInstanceState(listState); 
} 

... लेकिन onRestoreInstanceState() कभी नहीं कहा जाता हो जाता है। मैं भी विधि OnCreate() में राज्य को बहाल करने की कोशिश की, लेकिन यह रूप में अच्छी तरह नहीं बुलाया जाता है:

if (savedInstanceState != null) { 
    Parcelable listState = savedInstanceState.getParcelable("ListState"); 
    getExpandableListView().onRestoreInstanceState(listState); 
} 
+0

मैं इसी तरह यहाँ http://stackoverflow.com/questions/10611927/simplecursortreeadapter-and-cursorloader – toobsco42

+0

@ toobsco42 मैं नहीं दिख रहा है कि यह कैसे से संबंधित है कुछ कर रहा हूँ काम करना चाहिए प्रश्न – Tom

+0

पिछली गतिविधि पर लौटने पर एडाप्टर को फिर से सेट करने से सावधान रहें। एडाप्टर नया सेट अप सूची को पतन कर देगा। – jayeffkay

उत्तर

11

दुर्भाग्य से विस्तृत स्थिति हमेशा रीसेट करता है जब भी फोकस खो दिया है और जब यह फिर से onStart() फोकस हो जाता है लेकिन OnCreate() कहा जाता है नहीं है। तो अब के लिए मेरा कामकाज सभी विस्तारित वस्तुओं की आईडी को मैन्युअल रूप से संग्रहीत करना है और उन्हें फिर से स्टार्ट() में विस्तारित करना है। मैंने व्यवहार का पुन: उपयोग करने के लिए ExpandableListActivity का एक उप-वर्ग लागू किया।

public class PersistentExpandableListActivity extends ExpandableListActivity { 
    private long[] expandedIds; 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     if (this.expandedIds != null) { 
      restoreExpandedState(expandedIds); 
     } 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     expandedIds = getExpandedIds(); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     this.expandedIds = getExpandedIds(); 
     outState.putLongArray("ExpandedIds", this.expandedIds); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle state) { 
     super.onRestoreInstanceState(state); 
     long[] expandedIds = state.getLongArray("ExpandedIds"); 
     if (expandedIds != null) { 
      restoreExpandedState(expandedIds); 
     } 
    } 

    private long[] getExpandedIds() { 
     ExpandableListView list = getExpandableListView(); 
     ExpandableListAdapter adapter = getExpandableListAdapter(); 
     if (adapter != null) { 
      int length = adapter.getGroupCount(); 
      ArrayList<Long> expandedIds = new ArrayList<Long>(); 
      for(int i=0; i < length; i++) { 
       if(list.isGroupExpanded(i)) { 
        expandedIds.add(adapter.getGroupId(i)); 
       } 
      } 
      return toLongArray(expandedIds); 
     } else { 
      return null; 
     } 
    } 

    private void restoreExpandedState(long[] expandedIds) { 
     this.expandedIds = expandedIds; 
     if (expandedIds != null) { 
      ExpandableListView list = getExpandableListView(); 
      ExpandableListAdapter adapter = getExpandableListAdapter(); 
      if (adapter != null) { 
       for (int i=0; i<adapter.getGroupCount(); i++) { 
        long id = adapter.getGroupId(i); 
        if (inArray(expandedIds, id)) list.expandGroup(i); 
       } 
      } 
     } 
    } 

    private static boolean inArray(long[] array, long element) { 
     for (long l : array) { 
      if (l == element) { 
       return true; 
      } 
     } 
     return false; 
    } 

    private static long[] toLongArray(List<Long> list) { 
     long[] ret = new long[list.size()]; 
     int i = 0; 
     for (Long e : list) 
      ret[i++] = e.longValue(); 
     return ret; 
    } 
} 

शायद किसी के पास बेहतर समाधान हो सकता है।

0

इस प्रयास करें। यह समस्या का समाधान होगा, आंशिक रूप से

yourlist.setSaveEnabled(true); 
+0

दुर्भाग्य से यह कुछ भी नहीं बदलता है। – Tom

+0

यह केवल स्क्रीन के परिवर्तन अभिविन्यास के लिए काम करता है (यदि स्क्रीन के परिवर्तन अभिविन्यास के बाद एडाप्टर का डेटा पुनः लोड नहीं किया जाएगा) – user479211

2

यह

Parcelable state = exercisesList.onSaveInstanceState(); 
exercisesList.setAdapter(....); 
exercisesList.onRestoreInstanceState(state); 
+1

स्वीकृत उत्तर पूरी तरह से अनावश्यक है। यह सही तरीका है। – JanithaR

संबंधित मुद्दे