0

I have created one activity and i have stored my data in another class with encapsulation and from that class, I am not able to retrieve my data to the text field of another class. below is the code please help me so solve. when I run the app I am getting nothing on the screen.

Name.java

package com.example.voiceprescription;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.icu.text.IDNA;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.Locale;

public class Name extends AppCompatActivity {

    EditText et_Nname,et_Nage,et_Nphone;

    RadioButton rb_Nmale,rb_Nfemale;
    Button btn_Nnext;enter code here
    ImageButton ib_Nvoice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name);


        et_Nphone=findViewById(R.id.et_Nphone);
        et_Nname=findViewById(R.id.et_Nname);
        et_Nage=findViewById(R.id.et_Nage);
        ib_Nvoice=findViewById(R.id.ib_Nvoice);
        rb_Nfemale=findViewById(R.id.rb_Nfemale);
        rb_Nmale=findViewById(R.id.rb_Nmale);
        btn_Nnext=findViewById(R.id.btn_Nnext);


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

                String name=et_Nname.getText().toString().trim();
                String age=et_Nage.getText().toString().trim();
                String phone=et_Nphone.getText().toString().trim();
               PersonInfo personInfo=new PersonInfo();

               personInfo.setName(name);
               personInfo.setAge(age);
               personInfo.setPhone(phone);

               Intent intent=new Intent(Name.this,Symptoms.class);
               startActivity(intent);

            }
        });


    }
}

Personinfo.java

package com.example.voiceprescription;

public class PersonInfo {


    private String name;
    private String age;
    private String phone;
    private String gender;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

symptoms.java

package com.example.voiceprescription;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Symptoms extends AppCompatActivity {

    TextView tv1;
    PersonInfo personInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_symptoms);

        personInfo=new PersonInfo();
        tv1=findViewById(R.id.tv1);

        tv1.setText(personInfo.getName());

    }
}
nir doshi
  • 53
  • 6
  • What is your apps main activity? Which activity are you trying to launch that is ending up blank? Do you actually get nothing on the screen, or is it just after clicking the button on `Name` activity that `Symptoms` activity is blank? – Quinn Feb 19 '20 at 17:12
  • my main activity have just 3 buttons. Symptoms activity i am trying to launch. ya it is ending up blank , it have only only one textfield and it's getting blank. actually when i am not using button textfield is being displayed but when i launch it via intent it gets blank. – nir doshi Feb 19 '20 at 17:19

1 Answers1

0

This doesn't work this way what you have done. To do is pass the object as a bundle param to the symptoms activity.

Bundle bundle = new Bundle();

bundle.putSerializable("value", your_person_class_obj); intent.putExtras(bundle);

For this, your class must implement the Serializable interface.

public class PersonInfo implements Serializable {

}

Your code doesn't work because you set the object in one activity and it's not available for the second. Reference - Check this link for more details

Abhishek Dubey
  • 937
  • 8
  • 11