how to serialize/deserialize arrayList(Object)

I have an ArrayList<ItemList>

where ItemList is:

  • How can I get device ID for Admob
  • Android ExpandableListView using animation
  • setOnCancelListener and setOnDismissListener is not called for AlertDialog for back button pressed or touch outside
  • Enable Exception C++
  • How to disable SSLv3 in android for HttpsUrlConnection?
  • EditText and InputFilter cause repeating text
  • public class ItemList {
        public ArrayList<Item> it = new ArrayList<Item>();
        public String name = "";
    
        public ItemList() {
        }
    }
    

    and Item is:

    public class Item {
        public String name = "";
        public int count = 0;
    
        public Item() {
        }
    }
    

    I try to serialize this list:

    try {
                FileOutputStream fileOut = new FileOutputStream(sdDir + serFile);
                ObjectOutputStream out = new ObjectOutputStream(fileOut);
                out.writeObject(List_Of_Lists);
                out.close();
                fileOut.close();
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    I think it’s work, becouse I find this file in folder.

    But I can’t deserialize from file to ArrayList<ItemList>

    code:

            try {
                FileInputStream fileIn = new FileInputStream(sdDir + serFile);
                ObjectInputStream in = new ObjectInputStream(fileIn);
                List_Of_Lists = (ArrayList<ItemList>) in.readObject(); 
                Log.i("palval", "dir.exists()");
                in.close();
                fileIn.close();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    How I can deserialize this ArrayList<ItemList>?
    I always catch IOException.

  • How to change default text color using custom theme?
  • How do I pass extra variables during a search invoked by a SearchView/ Widget?
  • Accesing contents of res/raw programatically (Android)
  • Drawable-hdpi, Drawable-mdpi, Drawable-ldpi Android
  • Is possible use drawable-mdpi-fr, drawable-hdpi-fr, drawable-ldpi-fr, for localization with different resolutions
  • Build android release apk on Phonegap 3.x CLI
  • 3 Solutions collect form web for “how to serialize/deserialize arrayList(Object)”

    Your Item and ItemList classes needs to implements Serializable

    if you have made subclass then add serializabe method to parent class it will remove the error.

    I am assuming you have serialized the ItemList not the Item…..

    ArrayList<ItemList> arr = (ArrayList<ItemList>) in.readObject();
    
    for (ItemList a : arr) 
       {
            // In this loop by iterating arr, you will get the whole List of ItemList
    
       }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.