0

I've followed a tutorial on youtube on inserting data into Firebase database. I've created Member class:

package com.example.bazadanych;

public class Member {
    private String name;
    private String age;
    private String phoneNumber;
    private String height;

    public Member() {
    }

    public String getName() {
        return name;
    }

    public String getAge() {
        return age;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getHeight() {
        return height;
    }

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

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

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setHeight(String height) {
        this.height = height;
    }
}

And I created code in MainActivity:

public class MainActivity extends AppCompatActivity {
    EditText editName, editPhone, editAge, editHeight;
    Button btnSave;
    DatabaseReference ref;
    Member member;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editName = (EditText) findViewById(R.id.editName);
        editAge = (EditText) findViewById(R.id.editAge);
        editPhone = (EditText) findViewById(R.id.editPhone);
        editHeight = (EditText) findViewById(R.id.editHeight);
        btnSave = (Button) findViewById(R.id.buttonSave);
        ref = FirebaseDatabase.getInstance().getReference().child("Member");

        member = new Member();

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editName.getText().toString().trim();
                String age = editAge.getText().toString().trim();
                String height = editHeight.getText().toString().trim();
                String phone = editPhone.getText().toString().trim();

                member.setName(name);
                member.setAge(age);
                member.setHeight(height);
                member.setPhoneNumber(phone);

                ref.push().setValue(member);

            }
        });
    }

The problem is I can't see data on website. I enabled writing and reading by passing true value on the website. I've connected my app to Firebase and add database to my project. I don't know where the problem is. Anybody can help me?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • you can store data without creating a POJO class. You just have to pass a hashMap variable in setValue method – xaif May 10 '20 at 11:41
  • Have you tried to attach a complete listener to the setValue operation? – Alex Mamo May 10 '20 at 11:54
  • I've created a constructor with parameters and try to push it but it didn't work either... –  May 10 '20 at 12:05
  • I don't know how to pas a hashMap to be honest, can you show me how to? –  May 10 '20 at 12:07

1 Answers1

1

Your code seems correct. I've also checked it over my system and it is getting stored over the firebase. this is the output of your code
May be there is error in your activity_main.xml file.
Can you please provide your activity_main.xml file.

There is naming error in EditText id of phoneNumber in the activity_main.xml which is not same as in MainActivity.java when it is initialized in onCreate() method. Make both of them same.
See the difference
MainActivity.java and activity_main.xml

paul035
  • 327
  • 2
  • 8