Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I start learning Android programming recently. I have a problem with transferring data between two activities. In the first activity, some EditTexts take information from the user and the btn_apply button should save them in a list. The btn_show button should show the students_lastnames on the second activity but my code only shows the last last_name repeatedly. I don't know what is the problem?

What I have tried:

Main Activity:
    package com.example.mysixthandroidapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.NumberPicker;
    import android.widget.TextView;
    
    import java.io.Serializable;
    import java.util.ArrayList;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            EditText txt_name = (EditText)findViewById(R.id.txt_name);
            EditText txt_id = (EditText)findViewById(R.id.txt_lastname);
            EditText txt_filed = (EditText)findViewById(R.id.txt_field);
            EditText txt_university = (EditText)findViewById(R.id.txt_university);
            EditText txt_startyear = (EditText)findViewById(R.id.txt_startyear);
            
            Button btn_apply = (Button)findViewById(R.id.btn_apply);
            Button btn_show = (Button)findViewById(R.id.btn_show);
    
            Student std = new Student();
    
            TextView txt_result = (TextView)findViewById(R.id.txt_result);
    
            final ArrayList<Student> students  = new ArrayList<Student>();
            
            btn_apply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    std.setStudent_name(txt_name.getText().toString());
                    std.setStudent_lastname(txt_id.getText().toString());
                    std.setStudent_field(txt_filed.getText().toString());
                    std.setStudent_university(txt_university.getText().toString());
                    std.setStudent_startyear(txt_startyear.getText().toString());
                    students.add(std);
    
                }
            });
    
    
            btn_show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
                    Bundle arg = new Bundle();
                    arg.putSerializable("STUDENT", (Serializable)students);
                    myIntent.putExtra("BUNDLE", arg);
                    MainActivity.this.startActivity(myIntent);
    
    
                }
            });
        }
    }

Second Activity:

    package com.example.mysixthandroidapp;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    public class SecondActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            Intent intent  = getIntent();
            Bundle arg = intent.getBundleExtra("BUNDLE");
            ArrayList <Student> students_list = (ArrayList <Student>)arg.getSerializable("STUDENT");
            TextView txt_list = (TextView)findViewById(R.id.txt_list);
    
    
            for(int i = 0 ; i < students_list.size() ; i++){
    
                txt_list.setText("student: "+i + students_list.get(i).getStudent_lastname() + "\n");
            }
    
        }
    }

Student Class:
    package com.example.mysixthandroidapp;
    
    import java.io.Serializable;
    import java.util.ArrayList;
    
    
    public class Student implements java.io.Serializable {
        private String Student_name;
        private String Student_lastname;
        private String Student_field;
        private String Student_university;
        private String Student_startyear;
    
    
        public void setStudent_name(String name) {
    
            Student_name = name;
        }
        public void setStudent_lastname(String lname) {
    
            Student_lastname = lname;
        }
        public void setStudent_field(String field) {
    
            Student_field = field;
        }
        public void setStudent_university(String university) {
    
            Student_university = university;
        }
        public void setStudent_startyear(String startyear) {
    
            Student_startyear = startyear;
        }
    
    
        public String getStudent_name() {
    
            return Student_name;
        }
        public String getStudent_lastname() {
    
            return Student_lastname;
        }
        public String getStudent_field() {
    
            return Student_field;
        }
        public String getStudent_university() {
    
            return Student_university;
        }
        public String getStudent_startyear() {
    
            return Student_startyear;
        }
    
    
    
    }
Posted
Updated 22-Jun-21 10:00am

1 solution

It's because you create one instance of a Student:
Java
Student std = new Student();

And then every time you click your button you set new values to that same instance.
So each time you click the button, you overwrite the last set of data with the new stuff and add the same instance to your ArrayList.

What you need to do is create the instance just before you set it's properties, so you get a new one to add each time.

Think of it like a shirt: it has a pocket, but if you put your wallet in the pocket, it's fine. But you can't get two wallets in the same pocket, so to hold your mate's wallet you have to remove yours first. You want to hold both wallets, you need two shirts (one y=for you to wear, and one for your mate!)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900