0

I am trying to send an ID as an int and get it from an intent in another activity but it returns zero.

This is the intent in the first activity:

public class Main_page extends AppCompatActivity {
    ListView PatientList;
    Button BTaddPatient;
    DBpatients db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);
        PatientList = findViewById(R.id.PateintList);
        BTaddPatient = findViewById(R.id.ADDpateint);
        db = new DBpatients(this);

        BTaddPatient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Main_page.this, Add_patient_Activity.class);
                startActivity(intent);
            }
        });
        PatientList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Patient selected_patient = (Patient) parent.getItemAtPosition(position);

                Intent intent = new Intent(getApplicationContext(), Update_patient.class);
                intent.putExtra("ID", selected_patient.getId());
                intent.putExtra("name", selected_patient.getName());
                intent.putExtra("diagnose", selected_patient.getDiagnose());

                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        ArrayList<Patient> patients = db.getAllPatients();

        PatientAdapter patientAdapter = new PatientAdapter(this, R.layout.item_pateint, patients);
        PatientList.setAdapter(patientAdapter);
    }
}

And trying to get the ID in the other activity:

public class Update_patient extends AppCompatActivity {
DBpatients db;
EditText editName, editDiagnose;
Button UpdateBTN;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_update_patient);
    final int id =  getIntent().getExtras().getInt("ID");

    db = new DBpatients(this);

    editName = findViewById(R.id.EDname);
    editDiagnose = findViewById(R.id.EDdiagnose);
    UpdateBTN = findViewById(R.id.BTupdate);
    Patient patient = db.getPatientByID2(id);
    editDiagnose.setText(patient.getDiagnose());
    editName.setText(patient.getName());
    UpdateBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = editName.getText().toString();

            String diagnose = editDiagnose.getText().toString();

            Patient newPatient = new Patient( id, name, diagnose);
            db.UpdatePatient(newPatient);
            Toast.makeText(Update_patient.this, "successfuly UPDATED", Toast.LENGTH_SHORT).show();
        }
    });
}

This is Patient class :

public class Patient {
    private String name;
    private int id;

    private String diagnose;

    public Patient(String name, String diagnose) {
        this.name = name;
        this.diagnose = diagnose;
    }

    public Patient(int id, String name, String diagnose) {
        this.name = name;
        this.diagnose = diagnose;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDiagnose() {
        return diagnose;
    }

    public void setDiagnose(String diagnose) {
        this.diagnose = diagnose;
    }
}

The app crashes, and, by using the debugger, it shows that ID = 0 when receiving it.

Please help.

Saurabh
  • 434
  • 1
  • 4
  • 12
  • What exactly happens: Is the ID `0` set in the received intent or is no ID contained in the received intent? How do you start the activity from within `onItemClick`? – Michael Butscher Sep 13 '18 at 23:48
  • no ,by using debugger the id is set correctly in the intent but the problem when reciving it , it crashes and the value is zero when its recived – Mohammad Yousfy Sep 14 '18 at 00:17
  • Have you tried to call `getIntent().getExtras().containsKey("ID")` to check if there is really an ID contained in the received intent? Again: How do you start the activity from within `onItemClick`? – Michael Butscher Sep 14 '18 at 00:44
  • Take a look at the examples [here](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) and make sure your code is set up the same way. Also, if the app crashes include a log and indicate where/why it crashed since that's usually relevant. – Tyler V Sep 14 '18 at 02:47
  • show your Patient model class also – Bunny Sep 14 '18 at 05:12
  • Try to use int id = getIntent().getIntExtra("ID", 0); – Vishal Senjaliya Sep 14 '18 at 07:12
  • @MichaelButscher i`ve edited the code to show the full view and by using `code` getIntent().getExtras().containsKey("ID") `code` it should contain boolean and my value is int – Mohammad Yousfy Sep 14 '18 at 10:00
  • `getIntent().getExtras().containsKey("ID")` was meant only to check if the key is really present in the received intent or if the key isn't present and only the default value `0` for a missing key is returned by `getIntent().getExtras().getInt("ID")` – Michael Butscher Sep 14 '18 at 20:40

1 Answers1

1

You just have to send extras while calling your intent.

Like this:

Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Variable name", "Value you want to pass");
startActivity(intent);

Now on the OnCreate method of your SecondActivity you can fetch the extras like this.

If the value you sent was in int:

int value = getIntent().getIntExtra("Variable name which you sent as an extra", defaultValue(you can give it anything));

Example:

int value = getIntent().getIntExtra("ID", 0);
Manoj Patidar
  • 302
  • 3
  • 17