How to Save Custom Arraylist on Android Screen Rotate

How to save custom ArrayList on Android screen rotate?

You do not need to create a new class to pass an ArrayList of your custom objects. You should simply implement the Parcelable class for your object and use Bundle#putParcelableArrayList() in onSaveInstanceState() and onRestoreInstanceState(). This method will store an ArrayList of Parcelables by itself.


Because the subject of Parcelables (and Serializables and Bundles) sometimes makes my head hurt, here is a basic example of an ArrayList containing custom Parcelable objects stored in a Bundle. (This is cut & paste runnable, no layout necessary.)

Implementing Parcelable

public class MyObject implements Parcelable {
String color;
String number;

public MyObject(String number, String color) {
this.color = color;
this.number = number;
}

private MyObject(Parcel in) {
color = in.readString();
number = in.readString();
}

public int describeContents() {
return 0;
}

@Override
public String toString() {
return number + ": " + color;
}

public void writeToParcel(Parcel out, int flags) {
out.writeString(color);
out.writeString(number);
}

public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() {
public MyObject createFromParcel(Parcel in) {
return new MyObject(in);
}

public MyObject[] newArray(int size) {
return new MyObject[size];
}
};
}

Save / Restore States

public class Example extends ListActivity {
ArrayList<MyObject> list;

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

if(savedInstanceState == null || !savedInstanceState.containsKey("key")) {
String[] colors = {"black", "red", "orange", "cyan", "green", "yellow", "blue", "purple", "magenta", "white"};
String[] numbers = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};

list = new ArrayList<MyObject>();
for(int i = 0; i < numbers.length; i++)
list.add(new MyObject(numbers[i], colors[i]));
}
else {
list = savedInstanceState.getParcelableArrayList("key");
}

setListAdapter(new ArrayAdapter<MyObject>(this, android.R.layout.simple_list_item_1, list));
}

@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList("key", list);
super.onSaveInstanceState(outState);
}
}

Saving/Restoring custom ArrayList on Android screen rotate?

add this line in your Manifest in relevant activity tag , and activity will handle rotation itself

android:configChanges="screenSize|orientation|screenLayout" 

test it maybe help you

How do I save an ArrayList Uri on screen rotate?

store your data in onSaveInstanceState()

@Override
public void onSaveInstanceState (Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putSerializable("array", arrayDetails);
}

put these lines of code in OnCreate() method of your activity to get arrayList on Orientation Change.

if (savedInstanceState != null) {
arrayDetails = (ArrayList<yourListType>)savedInstanceState.getSerializable("array");
}

How do I save an ArrayList Uri on screen rotate?

store your data in onSaveInstanceState()

@Override
public void onSaveInstanceState (Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putSerializable("array", arrayDetails);
}

put these lines of code in OnCreate() method of your activity to get arrayList on Orientation Change.

if (savedInstanceState != null) {
arrayDetails = (ArrayList<yourListType>)savedInstanceState.getSerializable("array");
}

How to save ArrayList data and use in AutoCompleteTextView , after change device orientation

There is too small amount of code to tell for sure, but from what I see the problem may be in Loader implementation and its synchronization with the rest of the code.

Loaded loads data asynchronously. That means after you've rotated the device your code executed well, but the loader started to load once more. Before the loading actually started it calls onLoaderReset which contains adapter.clear() call. So all the data from adapter disappears thus AutocompleteTextView has nothing to show.

Also I can see in the onLoadFinished method you retrieve data but you do not set it into adapter - thus the adapter is empty.

One more time - I am not sure this is the reason since the code is incomplete, but I hope it will help you somehow.

How do I save all items in a custom ListView and their states on Screen Rotation?

Use a ListFragment instead of a ListView.

Tell the fragment to setRetainInstance and then reattach the fragment instead of creating a new one when you switch orientation.

Sam_D is incorrect when he says that you cannot use config changes if you use this method. getView is still called normally after which you can restore your listview back to the state you require (assuming you kept hold of your adapter in a private field within the fragment)

My ListView won't save upon rotating the screen; using parcelable interface and onSaveInstanceState

With this line

mAdapter = new BookAdapter(this, new ArrayList<Book>());

you are passing an empty list of Book to the adapter of your ListView. So your ListView will be empty, because it has nothing to show.

Then you save myClass that is never instantiated as I can see, so you are saving a null object. Then you restore this null object.

What you must do is:

  • create a list of Book and put them into an ArrayList
  • pass previously created ArrayList to your Adapter
  • to save data before rotation you must save your ArrayList into onSaveInstanceState method
  • to restore data after rotation you must reget your ArrayList into onRestoreInstanceState method
  • then recreate your Adapter with restored data and reassign it to your ListView

Update

Declare your ArrayList at Book myClass level ArrayList<Book> bookList;

Try split this line

mAdapter = new BookAdapter(this, new ArrayList<Book>());

into these two lines:

bookList = new ArrayList<Book>();
mAdapter = new BookAdapter(this, bookList);

then in your onSaveInstanceState

outState.putParcelableArrayList("bookList", bookList);

and in your onRestoreInstanceState

bookList = savedInstanceState.getParcelableArrayList("bookList");

After that you must recreate your Adapter and reassign it to your ListView

This should be the right way



Related Topics



Leave a reply



Submit