我正在尝试构建一个应用程序,卖方可以上传他的产品,并且用户可以看到它们 所以我希望买家在recyclerView中看到附近的产品 我正在使用Kotlin和Firebase实时数据库 这就是我尝试过的
class owner : AppCompatActivity() {
lateinit var databaseReference: DatabaseReference
lateinit var geoFire: GeoFire
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_owner)
val datbaseInstance = FirebaseDatabase.getInstance()
databaseReference = datbaseInstance.getReference("GOODS")
val id1 =databaseReference.push().key.toString()
floatingActionButton.setOnClickListener {
showDialogue(id1)
}
// I used this code to get the products but it didn't work and didn't show anything
val geoQuery0 : GeoQuery =
geofire!!.queryAtLocation(GeoLocation(currentLatitude, currentLongtude),
7.0)
geoQuery0.addGeoQueryEventListener(object : GeoQueryEventListener {
override fun onKeyEntered(key: String, location: GeoLocation) {
Log.i(TAG, String.format("Provider %s is within your search
range [%f,%f]", key, location.latitude, location.longitude))
databaseReference.addValueEventListener(object :
ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
TODO("Not yet implemented")
}
override fun onDataChange(p0: DataSnapshot) {
productAdapt.clear()
p0.children.forEach { children ->
val textMessss = children.getValue(uploadProduct::class.java)
val idd = textMessss?.id.toString()
productAdapt.add(
getProductAdapter(
this@owner,
children.getValue(uploadProduct::class.java)!!,
idd
)
)
}
}
})
}
override fun onKeyExited(key: String) {
Log.i(TAG, String.format("Provider %s is no longer in the
search area", key))
}
override fun onKeyMoved(key: String, location: GeoLocation) {
Log.i(TAG, String.format("Provider %s moved within the search
area to [%f,%f]", key, location.latitude, location.longitude))
}
override fun onGeoQueryReady() {
Log.i(TAG, "onGeoQueryReady")
}
override fun onGeoQueryError(error: DatabaseError) {
Log.e(TAG, "error: " + error)
}
})
}
fun showDialogue(id1:String) {
val alertbilder = AlertDialog.Builder(this, R.style.AlertDialog)
val view = layoutInflater.inflate(R.layout.adding_product, null)
alertbilder.setView(view)
val alertdialogue = alertbilder.create()
alertdialogue.show()
view.add_btn.setOnClickListener { val uploadP =
uploadProduct(id1,name,price,desc,uid,longtude,latitude,productImageUrl
, nameOfShop)
databaseReference.child(id1).setValue(uploadP)
val geoRef = databaseReference.child(id1)
geoFire = GeoFire(geoRef)
geoFire.setLocation("geoFire", GeoLocation(latitude, longtude))}
alertdialogue.dismiss()
}
}
这是我的适配器,用于接收数据并在recyclerView中显示它们
class getProductAdapter( val context: Context, val uploadProduct: uploadProduct, val idd: String ) :
Item() {
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.nameOfProductR.text = uploadProduct.name
viewHolder.priceOfProductR.text = uploadProduct.price
viewHolder.descOfProductR.text =uploadProduct.desc
val url =uploadProduct.image
GlideApp.with(context)
.load(url)
.placeholder(R.drawable.ic_launcher_background)
.into(viewHolder.productImage)
}
override fun getLayout() = R.layout.recycler_view_item
}
这就是数据在Firebase上的样子
GOODS
-M7Jr5Cb8bx97gtW5UCK
desc: "It Is Product One "
id: "M7Jr5Cb8bx97gtW5UCK"
image: ""
latitude: ""
longTude: ""
name: "Product 1"
nameOfShop: ""
price: "123"
uidd: ""
geoFire
g: "9q8yywdgue"
l
0: 37.7853889
1: -122.4056973
-M7KDGxfWJ3rJgNZSZrJ
desc: "Suwushd"
id: "M7KDGxfWJ3rJgNZSZrJ"
image: ""
latitude: ""
longTude: ""
name: "Sueuu"
nameOfShop: ""
price: "26262"
uidd: ""
geoFire
g: "9q8yywdgue"
l
这是实时数据库的规则
{
"rules": {
"<GOODS>": {
"$GOODS":{
// Allow anyone to read the GeoFire index
".read": true,
".write": true,
"geoFire":{
// Index each location's geohash for faster querying
".indexOn": ["g"]
}}},
".read": true,
".write": true,
}
}