android: how do i preserve the data in my arrayadapter/listview when change orientation?

后端 未结 6 620
醉梦人生
醉梦人生 2020-12-16 08:14

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

My adapter

public cl         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 08:53

    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)getLastNonConfigurationInstance();
        if(sResultArr == null) {
            sResultsArray = new ArrayList();  // or some other initialization
        }
    
        self.m_adapter = new ResultsAdapter(home.this, R.layout.listrow, sResultsArr,home.this);
    
        // ...
    }
    

提交回复
热议问题