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

后端 未结 6 609
醉梦人生
醉梦人生 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:35

    I solved this problem using OnSaveInstanceState() and OnRestoreInstanceState()

    @Override
        protected void onRestoreInstanceState(Bundle state){
            super.onRestoreInstanceState(state);
            name_array.addAll(state.getStringArrayList("key"));
            setListAdapter(arrayAdapter);
            arrayAdapter.notifyDataSetChanged();
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState){
            super.onSaveInstanceState(outState);
            outState.putStringArrayList("key",name_array);
        }
    

    name_array is hold in ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this ,R.layout.item,name_array);

    Now when you change from landscape to portrait and reverse, data aren't los

    0 讨论(0)
  • 2020-12-16 08:47

    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.

    0 讨论(0)
  • 2020-12-16 08:49

    Please put this code in your AndroidManifest.xml file.

    activity
            android:name="MainActivity"
            android:label="@string/app_name" 
            android:configChanges="keyboardHidden|orientation|screenSize"
    
    0 讨论(0)
  • 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<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);
    
        // ...
    }
    
    0 讨论(0)
  • 2020-12-16 08:54

    I think you're on the wrong way. Please take some time to see this video from Android's developers: http://www.youtube.com/watch?v=wDBM6wVEO70 (about 1 hour).

    I followed the video, and my list view works fine when screen changes orientation :-)

    0 讨论(0)
  • 2020-12-16 08:57

    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).

    0 讨论(0)
提交回复
热议问题