-1

been watching tutorials and forums, tried everything, still cant find the solution. When i retrieve data with getValue() and put it in object, it sees the values, but when i try to a the to a class with getValue(Kontaktas.class) the values are null. What am i doing wrong?

MAIN

    package com.juliusj.gym.easy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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.List;

public class MainActivity extends AppCompatActivity {

    DatabaseReference mRef, mIn;
    List<Kontaktas> mList;

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

        mList = new ArrayList<>();
        mRef = FirebaseDatabase.getInstance().getReference();
        //mRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://easy-683ba.firebaseio.com/users");
        mIn = mRef.child("users");
        mIn.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                //shake hands
                for (DataSnapshot unit : dataSnapshot.getChildren()){
                    Kontaktas value = unit.getValue(Kontaktas.class);
                    mList.add(value);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }
}

Kontaktas.java

    package com.juliusj.gym.easy;

public class Kontaktas {
    private String vardas;
    private String telefonas;

    public Kontaktas()
    {}

    public Kontaktas(String vardas, String telefonas){
        this.vardas = vardas;
        this.telefonas = telefonas;
    }

    public String getVardas() { return vardas; }

    public void setVardas(String vardas) {
        this.vardas = vardas;
    }

    public String getTelefonas() {
        return telefonas;
    }

    public void setTelefonas(String telefonas) {
        this.telefonas = telefonas;
    }
}

databasePIC

debugerLOG

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
JuliusJ
  • 21
  • 1
  • 9

1 Answers1

2

The error that I first see, is that your fields from your database and those from your model class are different! In your database your Telefonas field is capital T and in your model class you have telefonas with lower case t. Both fileds must be the same. You either change your the keys in your database, either change your model class. But i recommend you delete the actual data and add new data using your models class which is correct.

Also, if you are trying to print the values of your mList ArrayList outside the onDataChange() method, you'll get an empty list. This is because onDataChange() method has an asynchronous behaviour. To solve this, use the following code:

mIn.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<Kontaktas> mList = new ArrayList<>();
        for (DataSnapshot unit : dataSnapshot.getChildren()){
            Kontaktas value = unit.getValue(Kontaktas.class);
            mList.add(value);
        }
        Log.d("TAG", mList);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
});

If you want to use the mList outside onDataChange() method, you need to create your own callback and for that I recommend see the last part of my answer from this post.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you! The capital and lowercase letters changed it all. Can't believe i had spent 3 days trying to find the solution -_- – JuliusJ Feb 14 '18 at 12:58