views:

572

answers:

3

as above, is it done automatically? My list was empty once the orientation chMYanges? and nope, i need the orientation change =)

My adapter

public class ResultsAdapter extends ArrayAdapter<SearchItem> implements Filterable{

private ArrayList<SearchItem> subItems;
private ArrayList<SearchItem> allItems;// = new ArrayList<SearchItem>();
private LayoutInflater inflater;
private PTypeFilter filter;
private OnCheckedChangeListener test;

public ResultsAdapter(Context context, int textViewResourceId, ArrayList<SearchItem> items,OnCheckedChangeListener a) {

    super(context, textViewResourceId, items);
        //this.subItems = items;
        for( int i = 0;i < items.size();i++){
            subItems.add(items.get(i));
        }
        this.allItems = this.subItems;
        inflater= LayoutInflater.from(context);

        test = a;
}

My onCreate

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        



    ListView lvr = (ListView)findViewById(R.id.search_results);



  //  if(savedInstanceState == null){
        this.m_adapter = new ResultsAdapter(home.this, R.layout.listrow, sResultsArr,home.this);
        this.specsAdapter = new FeaturesExpandableAdapter(home.this,new ArrayList<String>(),new ArrayList<ArrayList<Feature>>()); 
   // }
        lvr.setAdapter(this.m_adapter);

thanks guys

+1  A: 

The data should not be lost when you change the orientation. The only thing I can imagine is that the layout for your activity is not correct, like, controls move out of the visible area upon changing the orientation.

Thorsten Dittmar
This seems highly possible- or perhaps the data that is being used to populate the list gets destroyed along with the along with the activity on the orientation change but does not get recreated?
Jack Patmos
hi thorsten,is it due to the fact that i actually used a custom arrayadapter for displaying and storing the data that the state of the adapter is not saved? Should i initiate my adapter in onCreate()?
joejy
hi jack i guess so, because i had a nullpointer exception to my adapter when i changed my orientation. What may be the reason that my adapter not recreate?
joejy
Hm. I'm using code from the Notes sample that comes with the API to populate a ListView with items from my database. That doesn't get destroyed upon changing the orientation. Maybe you're creating the ArrayAdapter in the "wrong place"? I.e., code that gets called not only creating the Activity by also upon changing the orientation? I can't imagine that the Activity should be destroyed and recreated upon reorienting the screen...
Thorsten Dittmar
Hi Thorsten: an orientation change by default will cause an Activity to be destroyed, see: http://developer.android.com/intl/de/reference/android/app/Activity.html#ConfigurationChangesJoejy: perhaps there is some data that you should be persisting in the onSaveInstanceState() of your activity and unpacking in the onRestoreInstanceState method? See the Activity documentation for more info.
Jack Patmos
does onSaveInstanceState() allows me to save non name-value pair stuff like objects ?
joejy
+3  A: 

If you have data in memory that needs to stick around during an orientation change, you need to do something to arrange that. The best solution is to implement onSaveInstanceState() and use it to store your data, because that gets used not only for orientation changes, but other situations as well (e.g., your activity is on the current stack, but it needs to get kicked out of RAM to free up space for things later in the stack).

CommonsWare
onsaveinstancestate i can only save name-value pairs.. may i know how do i keep the actual object itself?
joejy
Via `onSaveInstanceState()`, you can't, because your process may be destroyed (see the e.g. in my answer). You can use `onRetainNonConfigurationInstance()` to hold onto an arbitrary object during an orientation change, but bear in mind that this pretty much *only* handles orientation changes, not scenarios where your activity is evicted from RAM.
CommonsWare
+1  A: 

You can also use http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance()

Something like this in your Activity:

@Override
public Object onRetainNonConfigurationInstance() {
    return this.m_adapter.getItems();
}

And then in your onCreate():

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    // more init stuff here

    sResultsArr = (ArrayList<SearchItems>)getLastNonConfigurationInstance();
    if(sResultArr == null) {
        sResultsArray = new ArrayList<SearchItems>();  // or some other initialization
    }

    self.m_adapter = new ResultsAdapter(home.this, R.layout.listrow, sResultsArr,home.this);

    // ...
}
synic
thanks synic that that work just fine!
joejy