0

I want to get data from Firebase Realtime Database but the onDataChange() method looks as if its not executing. I put some breakpoints, but it looks like it never enters in onDataChange(). This is the database data

{
  "User" : {
    "0988123344" : {
      "Name" : "Eddy",
      "Password" : "1234"
    }
  }
}

I also checked the Logcat and there is no error.

    Button btnSignIn;
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference table_user = database.getReference("User");

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

        edtPassword = (MaterialEditText)findViewById(R.id.edtPassword);
        edtPhone = (MaterialEditText)findViewById(R.id.edtPhone);
        btnSignIn = (Button)findViewById(R.id.sbtnSignIn);
    }

    @Override
    protected void onStart() {
        super.onStart();
        btnSignIn.setOnClickListener(v -> table_user.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                User user = snapshot.child(edtPhone.getText().toString()).getValue(User.class);
                if(user.getPassword().equals(edtPassword.getText().toString())){
                    Toast.makeText(SignIn.this,"Success!",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(SignIn.this,"fail!",Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        }));
    }

And this is the User

    private String Name;
    private String Password;

    public User(String name, String password) {
        Name = name;
        Password = password;
    }

    public User() {
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }
}
  • Perform this task in onCreate method – Akshay Rajput May 06 '21 at 20:16
  • I tried it, is not working – Popescu Ion May 06 '21 at 20:17
  • Ok, please specify what you want to do along with your database structure – Akshay Rajput May 06 '21 at 20:20
  • I just want to get data from firebase into that user object. There is a picture of the database structure. – Popescu Ion May 06 '21 at 20:27
  • First of all, stop ignoring errors. Use `Log.d(TAG, error.getMessage());`. Do you get something printed out in the logcat? Please also edit your question and add your database structure as a JSON file. You can simply get it by clicking the Export JSON in the overflow menu (⠇) in your [Firebase Console](https://console.firebase.google.com/u/0/project/_/database/data). Please respond with @AlexMamo – Alex Mamo May 07 '21 at 08:46
  • @AlexMamo I added the database structure and in Logcat there is no error. This is the log if you want to take a look http://tiny-paste.com/91635/ – Popescu Ion May 07 '21 at 16:57
  • Please check the duplicate to see why you get no data and hot to solve it. – Alex Mamo May 09 '21 at 12:21
  • @AlexMamo It is not duplicate. The problem was a firebase bug, not my code. Also, why you marked it as duplicate when it is clearly not? My problem was that firebase did not respond to any query, not that a list adapter was wrong. Please investigate more before marking a question duplicate. Thank you! – Popescu Ion May 09 '21 at 12:42

1 Answers1

0

use this code

reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    User user = dataSnapshot.getValue(User.class);
                    Toast.makeText(MainActivity.this, "name: "+user.getName(), Toast.LENGTH_SHORT).show();
                    Toast.makeText(MainActivity.this, "password: "+user.getPassword(), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

And do this task in onCreate Method

Dharman
  • 30,962
  • 25
  • 85
  • 135
Akshay Rajput
  • 173
  • 1
  • 6