如何在Firebase中搜索数据? [英] How to search data in Firebase?
问题描述
Bundle bundle = getIntent()。getExtras();
String name = bundle.getString(name);
mValueView =(TextView)findViewById(R.id.textView);
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl(https://mymap-3fd93.firebaseio.com/Users);
com.google.firebase.database.Query query = mRef.child(Users)。orderByChild(title)。equalTo(name);
query.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
// Map< String,Object> map =( (< String,Object>)dataSnapshot.getValue();
// String Title =(String)map.get(title);
String Title = dataSnapshot.child(title)。 getValue()。toString();
mValueView.setText(Title);
$ b @Override
public void onCancelled(DatabaseError databaseError){
}
});
我想显示对象标题是相同的名称值。
这是Firebase数据库:
在您的代码中有两个问题:
- 您将指定子节点
Users
两次 - 查询结果列表结果,您的
onDataChange
不处理
指定子节点 Users
两次
mRef = FirebaseDatabase.getInstance()
。 getReferenceFromUrl( https://mymap-3fd93.firebaseio.com/Users);
// ^^^^^
查询查询= mRef.child(Users)。orderByChild(title)。equalTo(name);
// ^^^^^
轻松修正:
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl(https://mymap-3fd93.firebaseio.com/);
Query query = mRef.child(Users)。orderByChild(title)。equalTo(name);
我不确定为什么使用 getReferenceFromUrl()
开头。对于大多数应用程序来说,实现这一点很简单:
mRef = FirebaseDatabase.getInstance()。getReference();
查询结果列表
当您针对Firebase数据库执行查询时,可能会有多个结果。所以快照包含了这些结果的列表。即使只有一个结果,快照将包含一个结果列表。
query.addValueEventListener(new ValueEventListener( ){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
System.out.println(snapshot.getKey ));
System.out.println(snapshot.child(title)。getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError){
throw databaseError.toException();
}
});
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
mValueView = (TextView) findViewById(R.id.textView);
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/Users");
com.google.firebase.database.Query query = mRef.child("Users").orderByChild("title").equalTo(name);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Map<String,Object> map = (Map<String, Object>) dataSnapshot.getValue();
//String Title = (String) map.get("title");
String Title = dataSnapshot.child("title").getValue().toString();
mValueView.setText(Title);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
I want to show object title is same name value.
This is the Firebase database:
There are two problems in your code:
- you're specifying the child node
Users
twice - a query results a list of results, which your
onDataChange
doesn't handle
specifying the child node Users
twice
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/Users");
// ^^^^^
Query query = mRef.child("Users").orderByChild("title").equalTo(name);
// ^^^^^
Easily fixed:
mRef = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/");
Query query = mRef.child("Users").orderByChild("title").equalTo(name);
I'm not sure why you use getReferenceFromUrl()
to begin with. For most applications, this accomplishes the same is simpler:
mRef = FirebaseDatabase.getInstance().getReference();
a query results a list of results
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
System.out.println(snapshot.getKey());
System.out.println(snapshot.child("title").getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
这篇关于如何在Firebase中搜索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!