少年恃险若平地,独倚长剑凌清秋。这篇文章主要讲述如何使用firebase作为数据库在android中获取附近的用户名?相关的知识,希望能为你提供帮助。
【如何使用firebase作为数据库在android中获取附近的用户名()】我在下面添加了我的firebase数据库的屏幕截图。我正在使用geofire获取存储在electriciansAvailable中的附近用户位置lat / lng。我已经使用适配器显示ui中附近用户的列表,我能够显示密钥存储在电工中可用。
但我不想显示密钥,而是想显示用户的名字(例如:-san@gmail.com)。我很久以来一直坚持这个,请有人告诉我如何显示列表表单中附近用户名的名称?
下面我附上了我的截图和代码。它在db中将电工值更新为true但是电气名称在listview中显示为空白。我已经上传了屏幕截图和代码。
fun getElectriciansAround(){
getElectriciansArounStarted = true
val customerId: String = FirebaseAuth.getInstance().currentUser!!.uid
val driversLocation:DatabaseReference=FirebaseDatabase.getInstance().reference.child("electricianAvailable")
val geofire:GeoFire= GeoFire(driversLocation)
val geoQuery:GeoQuery = geofire.queryAtLocation(GeoLocation(mLastLocation.latitude,mLastLocation.longitude), 10000.0)
geoQuery.addGeoQueryEventListener(object : GeoQueryEventListener{
override fun onKeyEntered(key: String?, location: GeoLocation?) {
val electricianRef:DatabaseReference=FirebaseDatabase.getInstance().reference.child("users").child("Electricians").child(key!!)
driversLocation.addListenerForSingleValueEvent(object :ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onDataChange(dataSnapshot: DataSnapshot) {
if(dataSnapshot.exists() &
&
dataSnapshot.childrenCount>
0){
for(i in 0 until dataSnapshot.childrenCount){
electricianRef.child(customerId).setValue(true)
getDriverName("Electricians",key!!)
electricianNames= arrayOf(mElectricianName)
val adapter= ArrayAdapter(this@CustomerElectricianHome, android.R.layout.simple_expandable_list_item_1, electricianNames)
mListViewElectricians.adapter=adapter
}
}
}
})
Run code snippet
var mElectricianName:String=""
private fun getDriverName(ElectricianName: String, key: String) {
val mDriverNameDb:DatabaseReference=FirebaseDatabase.getInstance().reference.child("users").child(ElectricianName).child(key)
mDriverNameDb.addListenerForSingleValueEvent(object :ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(dataSnapshot: DataSnapshot) {
if(dataSnapshot.exists()){
val map:Map<
*,*>
= dataSnapshot.value as Map<
*,*>
if(map["name"]!=null){
mElectricianName= map["name"].toString()
}
}
}
})
}
[[在此处输入图片说明]
答案您需要使用
GeoQueryEventListener.onKeyEntered
方法内部的Firebase实时数据库中的额外读取来查找其他用户数据。就像是:
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
DatabaseReference electriciansRef = FirebaseDatabase.getInstance().getReference("users/Electricians");
electriciansRef.child(key).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue(String.class);
System.out.println(String.format("Electrician name: %s", name));
}@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
})
}...@Override
public void onGeoQueryError(DatabaseError error) {
System.err.println("There was an error with this query: " + error);
}
});
或者在Kotlin:
geoQuery.addGeoQueryEventListener(object:GeoQueryEventListener() {
fun onKeyEntered(key:String, location:GeoLocation) {
println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude))
val electriciansRef = FirebaseDatabase.getInstance().getReference("users/Electricians")
electriciansRef.child(key).addListenerForSingleValueEvent(object:ValueEventListener() {
fun onDataChange(dataSnapshot:DataSnapshot) {
val name = dataSnapshot.child("name").getValue(String::class.java)
println(String.format("Electrician name: %s", name))
}
fun onCancelled(databaseError:DatabaseError) {
throw databaseError.toException()
}
})
}...fun onGeoQueryError(error:DatabaseError) {
System.err.println("There was an error with this query: " + error)
}
})
推荐阅读
- Android通知会多次打开,而不是只打开一次
- Android(从运行时生成的文本字段中将数据保存到Firebase)
- 如何在Firebase中获取用户数作为计数在android TextView中
- 从Firebase实时数据库读取Android数据并在RecyclerView上显示
- 带有范围的Android Firebase查询(上方,中间,下方)
- 类'test_coredataAppDelegate'的重复接口声明
- 如何在Kali Linux中通过TOR路由所有机器流量
- 如何解决VirtualBox MacOS Mojave 10.14.3中的”无法连接到服务器” Mac App Store错误
- XAMPP虚拟主机可在Chrome和Firefox中使用,但不适用于Microsoft Edge浏览器