首先,我在很多地方搜索过,但我根本找不到这个问题的答案,所以,我在这里问。但是,我想知道:我如何在Android中的Firebase数据库中转换这样的查询: * SELECT a,b,c FROM MyTable * ?我试过的方法如下:
public void testQueryMethod(final PhisicianHandler handler) {
final String databaseReference = "CadastroMedico";
database.getReference(databaseReference)
.child("mDoctorID")
.child("mNome")
.child("mEspecialidade")
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
ArrayList<String> dadosMedico = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
dadosMedico.add(snapshot.getValue().toString());
System.out.println(snapshot.getValue().toString());
}
handler.trataMedico(dadosMedico);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
我的整个模特
公共类FirebaseDatabaseModel {
//Creating the instance of the database
private FirebaseDatabase database = FirebaseDatabase.getInstance();
public ArrayList<String> dataRetriever(final ArrayList<String> dadosMedicosRecebidos, final BaseAdapter adapter) {
final String tableName = "Especialidade";
final String childToBeOrdered = "NomesEspecialidades";
database.getReference(tableName).orderByChild(childToBeOrdered)
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
dadosMedicosRecebidos.add(snapshot.getValue().toString());
adapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return dadosMedicosRecebidos;
}
/**
* This method gets all the registries where specialty is equal to the selected item on the spinner
*/
public void queryMethod(String selectedItem, final PhisicianHandler handler) {
final String tableName = "CadastroMedico";
final String childToBeOrdered = "mEspecialidade";
database.getReference(tableName)
.orderByChild(childToBeOrdered)
.equalTo(selectedItem)
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
ArrayList<String> dadosMedico = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
dadosMedico.add(snapshot.getValue().toString());
}
handler.trataMedico(dadosMedico);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void userIdQueryMethod(final PhisicianHandler handler) {
final String tableName = "UserID";
final String childToBeOrdered = "ID/UserID";
database.getReference(tableName).orderByChild(childToBeOrdered)
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
ArrayList<String> dadosMedico = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
dadosMedico.add(snapshot.getValue().toString());
}
handler.trataMedico(dadosMedico);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void doctorIdQueryMethod(final PhisicianHandler handler) {
final String tableName = "DoctorID";
final String childToBeOrdered = "ID/DoctorID";
database.getReference(tableName).orderByChild(childToBeOrdered)
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
ArrayList<String> dadosMedico = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
dadosMedico.add(snapshot.getValue().toString());
}
handler.trataMedico(dadosMedico);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void testQueryMethod(String specialty, final PhisicianHandler handler) {
final String tableName = "CadastroMedico";
final String childToBeOrdered = "mEspecialidade";
DatabaseReference database = FirebaseDatabase.getInstance().getReference()
.child("CadastroMedico");
database.child("mDoctorID")
.child("mNome")
.child("mEspecialidade")
.orderByChild(childToBeOrdered)
.equalTo(specialty);
database.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
ArrayList<String> dadosMedico = new ArrayList<>();
for (DataSnapshot snapshot :
dataSnapshot.getChildren()) {
dadosMedico.add(snapshot.getValue().toString());
System.out.println(snapshot.getValue().toString());
}
handler.trataMedico(dadosMedico);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot,
String prevChildKey) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot,
String prevChildKey) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
但它对我不起作用。当我尝试在日志窗口中显示它时,我收到此消息:I / System.out:propertyValue:false并且我没有从我的基础获取任何值。我将添加一个图像,以便您可以看到我在说什么。
Image 1: The fields i'm talking about
Document: Screenshots of my whole database
我想做的是(例如在SQL中): * SELECT mDoctorID,mEmail,mEndereço来自CadastroMedico; *
我认为这是可能的,但我不知道该怎么做。我会很感激一些关于它的想法。提前谢谢。
答案 0 :(得分:0)
试试这个
DatabaseReference mDatabase=FirebaseDatabase.getInstance().getReference().child("CadastroMedico");
mDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
String doctorID=dataSnapshot.child("mDoctorId").getValue(String.class);
String mNome=dataSnapshot.child("mNome").getValue(String.class);
handler.trataMedico(dadosMedico);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
答案 1 :(得分:0)
创建一个类并声明要从其中的firebase获取的所有变量。在您的情况下,它们将是mDoctorID,mNome和mEspecialidade。
在课程关键字
@IgnoreExtraProperties
确保变量的名称与Firebase中的节点名称完全相同。它将如下所示:
@IgnoreExtraProperties
class CadastroMedico{
public String mDoctorID="", mNome="", mEspecialidade="";
}
现在调用firebase数据库查询,如果只想使用addListenerForSingleValueEvent
一次获取数据。假设您只需要一次数据,因为SELECT查询也只返回一次数据,代码看起来像这样:
reference.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<CadastroMedico> dadosMedico = new ArrayList();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
CadastroMedico cadastroMedico = snapshot.getValue(CadastroMedico.class)
dadosMedico.add(cadastroMedico);
System.out.println(cadastroMedico.mDoctorID + " " + cadastroMedico.mNome + " " +cadastroMedico.mEspecialidade);
}
//modify your handler accordingly
}
@Override
public void onCancelled(DatabaseError databaseError) {
//handle error
}});