Android: how to save data in my arrayadapter / listview file when orientation changes?

as stated above, is this done automatically? Was my list empty after targeting chMYanges? and no, I need a change in orientation =)

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

+3
source share
6 answers

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

    // ...
}
+20
source

, , - , . - onSaveInstanceState() , , (, , , ).

+5

, . , Android: http://www.youtube.com/watch?v=wDBM6wVEO70 ( 1 ).

, , : -)

+1

, OnSaveInstanceState() 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 ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this ,R.layout.item,name_array);

, , los

0

. , , , , , .

-1

, AndroidManifest.xml .

activity
        android:name="MainActivity"
        android:label="@string/app_name" 
        android:configChanges="keyboardHidden|orientation|screenSize"
-5

Source: https://habr.com/ru/post/1734531/


All Articles