0

I have a problem. I'm working on some app and i want to enable user to remember his favorite stores. I'm wanted to do this whit a file that he create on his phone and where he add object of the store. I tried something like this:

favorites.setAddress(listPlace.get(0).get("address"));
            favorites.setId(String.valueOf(id));
            favorites.setName(imeRestorana);            
            myFavorites.add(favorites);
            String FILENAME = "myFavorites";
            try {
                FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(myFavorites);
                oos.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

And then i will read that from a file like this and insert it into a list:

String FILENAME = "myFavorites";
                ArrayList<Favorites> myF = new ArrayList<Favorites>();
                try {
                    FileInputStream fis = openFileInput(FILENAME);
                    ObjectInputStream ois = new  ObjectInputStream(fis);                    
                    myF = (ArrayList<Favorites>) ois.readObject();
                    ois.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }           
                myFavorites = new ArrayList<HashMap<String,String>>();
                for(int i = 0; i<myF.size(); i++){
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("Ime", myF.get(0).getName());
                    map.put("Adresa", myF.get(0).getAddress());
                    map.put("ID", myF.get(0).getId());
                    map.put("Latitude", myF.get(0).getLatitude());
                    map.put("Longitude", myF.get(0).getLongitude());
                    myFavorites.add(map);
                }

                ListAdapter adapter = new SimpleAdapter(this, myFavorites, R.layout.listviewfavoriti, new String[]{"Ime","Adresa"},
                        new int[]{R.id.textViewFavoritiNaziv, R.id.textViewFavoritiAdresa});
                listView.setAdapter(adapter);

My problem is that it shows only 1 store...because it reads only first object... How can I change that????

Jovan
  • 1,733
  • 3
  • 19
  • 38
  • 1
    Why not use a database? It integrates much better with the Android UI framework (e.g. `CursorAdapter`), also persistently stores data (in a file) and allows also kinds of insert, select and filter operations. – Philipp Reichart Sep 08 '11 at 12:09
  • in the beginning it seemed easier than database...is this posible or i must use datebase??? – Jovan Sep 08 '11 at 12:28
  • It's definitely possible to use serialized objects in a file, but database might be easier in the long run. – Philipp Reichart Sep 08 '11 at 13:58
  • Favorite class is serialized, but how can i get all object from file??? I get only first... – Jovan Sep 08 '11 at 14:18
  • You cannot easily *append* to an ObjectOutputStream once it's closed (even if you open the file in append mode) -- if you really want that, check out http://stackoverflow.com/questions/1194656/appending-to-an-objectoutputstream --- I still strongly suggest you use Android's built-in SQLite database library instead. – Philipp Reichart Sep 08 '11 at 14:24

0 Answers0