我有一个应用程序,我试图在活动之间传递类对象。我就是这样做的。
班级:
public class Player implements Serializable{
public String name;
public int score;
public static final int serialVersionUID = 12345;
}
将类对象置于额外的意图:
private TextView createNewTextView (String text){
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView newTextView = new TextView(this);
newTextView.setLayoutParams(lparams);
newTextView.setText(text);
Player newPlayer = new Player();
newPlayer.name = text;
newPlayer.score = 0;
players.add(text);
playerScores.add(newPlayer);
zacniIgro.putExtra("playerScores", (ArrayList<Player>) playerScores);
zacniIgro.putStringArrayListExtra("players", (ArrayList<String>) players);
return newTextView;
}
在另一个活动中获得额外的意图:
playersData = getIntent();
playerScoresData = getIntent();
players = playersData.getStringArrayListExtra("players");
playerScores = (ArrayList<Player>) playerScoresData.getSerializableExtra("playerScores");
我现在如何操作那些可序列化的对象?我想从 playerScores 中获取某个元素并对其进行操作。例如:我想从中取出索引为 0 的元素,然后使用它的名称和分数进行操作。