[英]Android - Passing an ArrayList of Custom Objects to Another Activity
[英]Passing multiple objects to another activity
我正在嘗試創建一個非常基本的應用程序,該應用程序基本上可以跟蹤4個在現實生活中玩紙牌游戲的玩家的得分。 我的想法是為每個玩家創建一個“玩家”類的實例,目前該實例僅包含變量“名稱”和“分數”。 我遵循了本文的說明( http://sohailaziz05.blogspot.de/2012/04/passing-custom-objects-between-android.html ),並在“玩家”類中實現了Parcelable:
public class Player implements Parcelable {
private String name;
private int score;
public Player(String name, int score) {
this.name = name;
this.score = score;
}
public String getPlayerName() {
return name;
}
public int getPlayerScore() {
return score;
}
public Player(Parcel in) {
String[] data = new String[2];
in.readStringArray(data);
this.name = data[0];
this.score = Integer.parseInt(data[1]);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[]{this.name, String.valueOf(this.score)});
}
public static final Parcelable.Creator<Player> CREATOR = new Parcelable.Creator<Player>() {
@Override
public Player createFromParcel(Parcel source) {
return new Player(source); //using parcelable constructor
}
@Override
public Player[] newArray(int size) {
return new Player[size];
}
};
}
這是第一個活動的代碼(我在這里僅創建一個玩家,理想情況下應為4個或更多):
public class CreatePlayersScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_players_screen);
}
public void startGame(View view) {
EditText editText1 = (EditText) findViewById(R.id.editText1);
String namePlayer1 = editText1.getText().toString();
int scorePlayer1=0;
Player player1 = new Player(namePlayer1, scorePlayer1);
Intent intent=new Intent(this,ScoreScreen.class);
intent.putExtra("EXTRA_PLAYER_1",player1);
startActivity(intent);
}
}
當我在第一個活動中按下按鈕時,將觸發“ startGame”,並且應該在第二個活動中顯示玩家名稱和分數:
public class ScoreScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score_screen);
Player player1= getIntent().getParcelableExtra("EXTRA_PLAYER_1");
String namePlayer1 = player1.getPlayerName();
TextView textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(namePlayer1);
int scorePlayer1 = player1.getPlayerScore();
TextView textView5 = (TextView) findViewById(R.id.textView5);
textView5.setText(scorePlayer1);
}
}
因此,當我運行此命令時,按下按鈕從第一個活動轉到第二個活動,就會在模擬器中收到以下錯誤消息: 游戲已停止再次打開應用程序
怎么了? 可打包是正確的方法還是我應該使用可序列化的? 我如何不僅可以將1個對象還可以將4個對象傳遞給第二個主動性?
非常感謝您的幫助,我真的很困在這里...謝謝!
這就是我通常的做法。
public void startGame(View view) {
EditText editText1 = (EditText) findViewById(R.id.editText1);
String namePlayer1 = editText1.getText().toString();
int scorePlayer1 = 0;
Player player1 = new Player(namePlayer1, scorePlayer1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
String namePlayer2 = editText2.getText().toString();
int scorePlayer2 = 0;
Player player2 = new Player(namePlayer2, scorePlayer2);
Intent intent = new Intent(this, ScoreScreen.class);
Bundle bundle = new Bundle();
bundle.putParcelable("EXTRA_PLAYER_1", player1);
bundle.putParcelable("EXTRA_PLAYER_2", player2);
intent.putExtras(bundle);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Player player1;
Player player2;
if (getIntent().getExtras() != null) {
player1 = getIntent().getExtras().getParcelable("EXTRA_PLAYER_1");
player2 = getIntent().getExtras().getParcelable("EXTRA_PLAYER_2");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.