开发者

How to read more than one object from a file in android

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,开发者_如何学JAVAString>>();
                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????

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜