简体   繁体   中英

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.

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. Can anybody say where i am wrong?

You're getting the wrong extra. You need to get the extra with the key 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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