保存ListView的列表

时间:2015-01-02 21:37:51

标签: android listview android-activity android-listview gson

以下是清单的声明:

public List<gameRowItem> GameRowItem;

这是setOnItemClickListener代码:

gameListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if(position == plusPosition) //when plusSign is clicked
            {

                someGame = new Game();  //instance of game class
                someGame.gameName = preferences.getString("Game Name", null);
                gson = new Gson();

                jsonOfSomeGame = gson.toJson(someGame);       // put someGame into json
                editor.putString("someGame", jsonOfSomeGame);  //put some game into sharedPreferences
                editor.commit();

                jsonOfSomeGame = preferences.getString("someGame", null);  //receive json of some game
                savedSomeGame = gson.fromJson(jsonOfSomeGame, Game.class);  // turn json into object of game
                arrayOfGames.add(0, savedSomeGame);

                jsonOfNewGame = gson.toJson(newGame);
                editor.putString("newGame", jsonOfNewGame);
                editor.commit();

                jsonOfNewGame = preferences.getString("newGame", null);
                savedNewGame = gson.fromJson(jsonOfNewGame, gameRowItem.class);
                savedNewGame = new gameRowItem(savedSomeGame.gameName);

                jsonOfGameRowItem = gson.toJson(GameRowItem);
                editor.putString("GameRowItem", jsonOfGameRowItem);
                editor.commit();

                jsonOfGameRowItem = preferences.getString("GameRowItem", null);
                gameRowItem[] savedGameRowItem = gson.fromJson(jsonOfGameRowItem, gameRowItem[].class);



                GameRowItem.add(0, savedNewGame);
                adapter.notifyDataSetChanged();

                plusPosition++;


            }else{
                Intent toGamePlay = new Intent(ListOfGames.this, GamePlay.class);
                startActivity(toGamePlay);
            }


        }
    });

我正在尝试将项目保存在ListView上,这样即使我离开Activity,它仍然会存在。正如您可能看到的那样,我一直在尝试使用Gson来存储对象,希望它能将项目保存在ListView上。但是,我现在意识到我应该尝试保存GameRowItem的实际List,我只是不确定如何去保存GameRowItem。我甚至试图将GameRowItem保存到代码的底部,但是没有成功。如果您需要更多代码,请随时提出任何帮助。

1 个答案:

答案 0 :(得分:0)

为UI和数据(模型和列表)创建单独的类。

您已拥有自己的模型:Game。使用方法创建GamesList,从存储中加载Game列表,保存列表,添加新的Game等。

然后在您的活动中,加载GamesList并填充ListView。点击加号后,将新GameRowItem添加到GamesList并保存。

我更喜欢将列表保存到私有文件而不是SharedPreferences,因为它很容易存储实现Serializable的对象。

public class Game implements Serializable {
    private String name;
    // ...
}


public class GamesList {
    private static final String FILENAME = "games.ser";

    private List<Game> list;

    public GamesList() {
        list = new ArrayList<Game>();
    }

    public int size() {
        return list.size();
    }

    public GamesList add(Game item) {
        // Add at the front of the list
        list.add(0, item);

        return this;
    }

    public Game get(int position) {
        return list.get(position);
    }

    public void remove(int position) {
        list.remove(position);
    }

    @SuppressWarnings("unchecked")
    public void loadFromCache(Context context) {
        try {
            InputStream in = context.openFileInput(FILENAME);

            ObjectInputStream objIn = new ObjectInputStream(in);
            list = (List<Game>) objIn.readObject();
            objIn.close();
        } catch (Exception e) {
        }
    }

    public void saveToCache(Context context) {
        try {
            FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
            ObjectOutputStream out = new ObjectOutputStream(fos);
            out.writeObject(list);
            out.close();
        } catch (IOException e) {
        }
    }
}


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGamesList = new GamesList();
    mGamesList.loadFromCache(this);
}

gameListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if(position == plusPosition) //when plusSign is clicked
        {

            someGame = new Game();  //instance of game class
            // fill game per your logic
            mGamesList.add(someGame).saveToCache(ListOfGames.this);

            adapter.notifyDataSetChanged();

            plusPosition++;


        }else{
            Intent toGamePlay = new Intent(ListOfGames.this, GamePlay.class);
            startActivity(toGamePlay);
        }


    }
});