簡體   English   中英

傳遞ArrayList使用意圖

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM