[英]Passing ArrayList use Intent
我的第一个活动:
btnResetSt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edtStId.setText("");
edtStName.setText("");
edtStDoB.setText("");
edtStEmail.setText("");
edtStPhone.setText("");
edtStAddress.setText("");
edtStBatch.setText("");
Toast.makeText(AddStudent.this,students.size()+"",Toast.LENGTH_LONG).show();
Intent intent = new Intent(AddStudent.this, StudentManage.class);
intent.putExtra("StudentList",students);
startActivity(intent);
}
});
添加学生的功能:
btnAddSt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Student st = new Student();
st.setStdId(edtStId.getText().toString());
st.setStdName(edtStName.getText().toString());
st.setStdDoB(edtStDoB.getText().toString());
st.setStdEmail(edtStEmail.getText().toString());
st.setStdPhone(edtStPhone.getText().toString());
st.setStdAddress(edtStAddress.getText().toString());
st.setStdBatch(edtStBatch.getText().toString());
students.add(st);
}
});
在将学生添加到ArrayList之后,我想使用意图通过btnResetSt传递它。
这是我的第二项活动 :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_manage);
Anhxa();
Intent intent = getIntent();
if (intent.getSerializableExtra("students")!=null) {
students = (ArrayList<Student>) intent.getSerializableExtra("students");
stAdt = new StudentAdapter(this, R.layout.student_line, students);
lstvSt.setAdapter(stAdt);
stAdt.notifyDataSetChanged();
}else {Toast.makeText(this,"Null",Toast.LENGTH_LONG).show();}
}
我用一个Toast.makeText检查intent.getSerializableExtra的结果,它真的空,所以我不能显示我的第二个活动列表列表视图。 谁能说我错了?
您得到了错误的额外收入。 您需要使用键StudentList
获得更多StudentList
。
因此它必须是:
students = (ArrayList<Student>) intent.getSerializableExtra("StudentList");
为避免相同的错误,请在目标活动中使用静态键。 像这样:
public class SecondActivity extends Activity {
public static final String STUDENT_LIST_EXTRA = "StudentList";
...
}
因此,您可以使用以下内容添加多余的内容:
intent.putExtra(SecondActivity.STUDENT_LIST_EXTRA, students);
并且,使用以下命令获得额外的收益:
students = (ArrayList<Student>) intent.getSerializableExtra(STUDENT_LIST_EXTRA);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.