0

我有个问题。我正在开发一些应用程序,我想让用户记住他最喜欢的商店。我想用他在手机上创建的文件以及在其中添加商店对象的文件来执行此操作。我试过这样的事情:

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();
            }

然后我将从这样的文件中读取它并将其插入到列表中:

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);

我的问题是它只显示一个商店......因为它只读取第一个对象......我该如何改变它????

4

0 回答 0