需要在activity之间传递数据,遇到一些困难。代码:private TextView createNewTextView (String text){
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView newTextView = new TextView(this);
ArrayList<String> players = new ArrayList<String>();
Intent zacniIgro = getIntent();
newTextView.setLayoutParams(lparams);
newTextView.setText(text);
players.add(text);
zacniIgro.putStringArrayListExtra("players", players);
return newTextView;
}
public void zacniIgro (View v){
Intent zacniIgro = new Intent (getApplicationContext(), Igra.class);
startActivity(zacniIgro);
}在新的activity中如何得到数据?我试过:ArrayList<String> players = data.getStringArrayListExtra("players");检索列表:@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_igra);
ArrayList<String> players = data.getStringArrayListExtra("players");
}给data加了红下划线。不知道是什么错误。
2 回答
慕少森
TA贡献2019条经验 获得超9个赞
因为你每次开始activity都创建了一个intent。
ArrayList<String> players = new ArrayList<String>(); //declare it outside of the function 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); players.add(text); return newTextView; } public void zacniIgro (View v){ Intent zacniIgro = new Intent (getApplicationContext(), Igra.class); zacniIgro.putStringArrayListExtra("players", players); startActivity(zacniIgro); }
在另一个activity中:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_igra); Intent data = getIntent(); ArrayList<String> players = data.getStringArrayListExtra("players"); }
添加回答
举报
0/150
提交
取消