简体   繁体   English

传递ArrayList使用意图

[英]Passing ArrayList use Intent

My first activity: 我的第一个活动:

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);
        }
    });

Function to add Student: 添加学生的功能:

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);

        }
    });

After add a student to ArrayList, i want passing it throught btnResetSt use Intent. 在将学生添加到ArrayList之后,我想使用意图通过btnResetSt传递它。

Here is my Second activity : 这是我的第二项活动

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();}
}

I use a Toast.makeText to check result of intent.getSerializableExtra and it realy Null so i can't display my list in second activity to List View. 我用一个Toast.makeText检查intent.getSerializableExtra的结果,它真的空,所以我不能显示我的第二个活动列表列表视图。 Can anybody say where i am wrong? 谁能说我错了?

You're getting the wrong extra. 您得到了错误的额外收入。 You need to get the extra with the key StudentList . 您需要使用键StudentList获得更多StudentList

So it must be: 因此它必须是:

students = (ArrayList<Student>) intent.getSerializableExtra("StudentList");

To prevent the same mistake, use a static key in the target Activity. 为避免相同的错误,请在目标活动中使用静态键。 Something like this: 像这样:

public class SecondActivity extends Activity {
  public static final String STUDENT_LIST_EXTRA = "StudentList";

  ...
}

So, you can use the following to put the extra: 因此,您可以使用以下内容添加多余的内容:

intent.putExtra(SecondActivity.STUDENT_LIST_EXTRA, students);

And, use the following to get the extra: 并且,使用以下命令获得额外的收益:

students = (ArrayList<Student>) intent.getSerializableExtra(STUDENT_LIST_EXTRA);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM