一直在看教程和论坛,尝试了所有方法,仍然找不到解决方案。当我使用 getValue() 检索数据并将其放入对象中时,它会看到这些值,但是当我尝试使用 getValue(Kontaktas.class) 到一个类时,值是空的。我究竟做错了什么?
主要的
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;
}
}