0

I have a class that contains an ArrayList<View>. The Class works for me but i'm not able to save(to file locally) it yet. Is there any possibility to to that? I tried Serializable and Externalizable but always get a NotSerializableException. Setting to transient did not work either. That is the code (I know Serializable won't work):

public class ViewsData implements Serializable {

    private  ArrayList<View> viewsList = new ArrayList<View>();

    public ViewsData () {

    }


    public ArrayList<View> getViewsList() {
        return viewsList;
    }

    public void addToViewsList(View view) {     
        viewsList.add(view);        
    }
}

I'm very thankful for any hint about that.

iks23
  • 1
  • 3
  • You should implement Parcelable. http://stackoverflow.com/questions/7042272/how-to-properly-implement-parcelable-with-an-arraylistparcelable – Michał Z. Oct 19 '13 at 12:14
  • According to this:http://stackoverflow.com/questions/15448623/save-parcelable-class-into-file you should not do that. – iks23 Oct 19 '13 at 12:19
  • Oh, I didn't know that you want to save it to file. I thought that you just want to marshall it somehow. So I think that you can implement Serializable and during serialization just iterate over arraylist and save each element independently. Deserialization will be just in opposite way: creating arraylist and fill it with data. – Michał Z. Oct 19 '13 at 13:26
  • No, it doestn accept View-Elements (like EditText, Button, TextView and s.o.)..that is exactly my problem. They dont implement Serializable. – iks23 Oct 19 '13 at 13:28
  • But why do you actually want to save it in file? Because serializing a View and deserializing it is not a good idea. A View has a context, which can be destroyed in the time between you commit the serialized version and the time you deserialize it. – Michał Z. Oct 19 '13 at 13:38
  • Yea, i guess that will be the issue about that. Thank you! – iks23 Oct 19 '13 at 13:52

1 Answers1

0

I have done something quite like what you are trying to do and this was my way to solve it.

What I did was using a database to store all information. And a new class that created views of this information.

Everytime I started the application I had to recreate the ArrayList from the database strings. But this was very handy in my case.

When you add a View in the ArrayList, make sure you go thru the database and not just insert a View.

Adam Wigren
  • 383
  • 2
  • 11
  • Well yea i actually have something like that. But i thought that its maybe better code to store the views directly in an ArrayList than in a List with infos about how to create them... – iks23 Oct 19 '13 at 13:51
  • Then you are on it. Generally you don't want to save Views to file because a View uses a Context that might change. – Adam Wigren Oct 19 '13 at 14:03