如何在android中恢复parcelable列表

时间:2017-07-03 16:41:20

标签: android parcel

我有这个应用程序,在一个活动中创建一个玩家列表,并在列表视图中显示它们。当我创建一个播放器时,我使用populatePlayerList方法,它工作正常。但是当我将列表保存在一个包裹中并尝试恢复它时,它不会填充列表视图。无法弄清楚为什么会这样。以下是一些代码:

public class PlayerManager extends AppCompatActivity {

            EditText playerName, playerNumber, playerPosition;
            ArrayList<Player> players = new ArrayList<Player>();
            ListView playerListView;


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

                setContentView(R.layout.activity_player_manager);

                playerName = (EditText) findViewById(R.id.etPlayerName);
                playerNumber = (EditText) findViewById(R.id.etPlayerNumber);
                playerPosition = (EditText) findViewById(R.id.etPlayerPosition);
                playerListView = (ListView) findViewById(R.id.playerList);

                if(savedInstanceState!=null){
                    players = savedInstanceState.getParcelableArrayList("savedList");
                    populatePlayerList();
                }

                final Button addPlayerBtn = (Button) findViewById(R.id.bAddPlayer);

                addPlayerBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        addPlayer(playerName.getText().toString(), playerNumber.getText().toString(), playerPosition.getText().toString());
                        populatePlayerList();
                        Toast.makeText(getApplicationContext(), playerName.getText().toString() + " added!", Toast.LENGTH_SHORT).show();
                    }
                });          

            private void populatePlayerList(){
                ArrayAdapter<Player> adapter = new PlayerListAdapter();
                playerListView.setAdapter(adapter);
            }

            private void addPlayer(String name, String number, String position){
                players.add(new Player(name, number, position));
        }

        private class PlayerListAdapter extends ArrayAdapter<Player> {
                public PlayerListAdapter(){
                    super(PlayerManager.this, R.layout.player_list, players);
                }

                @Override
                public View getView(int position, View view, ViewGroup parent){
                    if (view == null)
                        view = getLayoutInflater().inflate(R.layout.player_list, parent, false);

                    Player currentPlayer = players.get(position);

                    TextView name = (TextView) view.findViewById(R.id.playerName);
                    name.setText(currentPlayer.get_name());
                    TextView number = (TextView) view.findViewById(R.id.playerNumber);
                    number.setText(currentPlayer.get_number());
                    TextView playerPosition = (TextView) view.findViewById(R.id.playerPosition);
                    playerPosition.setText(currentPlayer.get_position());

                    return view;
                }
            }

    @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putParcelableArrayList("savedList", players);
        }

这是我的玩家类:

public class Player implements Parcelable{

    private String name, position,number;

    public Player (String name, String number, String position) {
        this.name = name;
        this.number = number;
        this.position = position;
    }

    protected Player(Parcel in) {
        name = in.readString();
        position = in.readString();
        number = in.readString();
    }

    public static final Parcelable.Creator<Player> CREATOR = new Parcelable.Creator<Player>() {
        @Override
        public Player createFromParcel(Parcel in) {
            return new Player(in);
        }

        @Override
        public Player[] newArray(int size) {
            return new Player[size];
        }
    };


    public String get_name() {
        return name;
    }

    public String get_position() {
        return position;
    }

    public String get_number(){
        return number;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(position);
        dest.writeString(number);
    }
}

当我尝试恢复它并且它是空的时,我检查了玩家列表。所以我猜它永远不会保存在outState中。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我通过添加android修复了我的问题:launchMode =&#34; singleInstance&#34;在Android Manifest.xml文件中的PlayerManager活动中。并且我重写onBackPressed以将我发送回上一个活动,因此不会调用onDestroy方法。这样我就可以在PlayerManager活动中保留相同的实例。