我试图从Firebase加载一组特定的数据(用户搜索城市,我们获取最近的位置并将其存储在一个数组中)并且我有一个问题填充对我的结果进行回收查看。我收到了这个错误:com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.android.projectrc.Listing
- 这似乎表明我得到了Strings而不是"列表" - 考虑到数据库的结构如下,这是有道理的:
Listings
{
"-L01KIjNtpgq2H0VopCn" : {
"LevelOfQuestionnaireCompletion" : true,
"ListingData" : {
"AircraftHouseRules" : {
"Pricing" : "100",
"SuitableForPets" : "Yes",
"SuitableForSmoking" : "No",
"SuitableForchildren" : "Yes"
},
"BasicInfo" : {
"AirportCode" : "ATL",
"City" : "Atlanta",
"Country" : "United States",
"ModelPlane" : "Learjet",
"NameOfListing" : "Roomy 4- Seater",
"NumberOfGuests" : "0",
"Owner" : "WdWB80uRtUPRF2sOFH5uJr2Tfu12",
"PrimarilySetupForGuests" : "Yes",
"State" : "Georgia",
"Street" : "6000 N Terminal PKWY",
"ZipCode" : "30320"
},
"DetailedInformation" : {
"IdealGuests" : "Big groups",
"ImageRef" : {
"-L01KUEWb8trSidSTzgW" : "5051E390-7FD5-4CEF-9C1B-EB959C13A4C2",
"-L01KUEWb8trSidSTzgX" : "E2BCA170-0F7B-43B1-BAC6-BEBBB6DEF630"
},
"SummaryOfPlane" : "Give us a brief description of your plane..."
}
}
},
"-L035Y5c_i88HW-BOlbQ" : {
"LevelOfQuestionnaireCompletion" : true,
"ListingData" : {
"AircraftHouseRules" : {
"Pricing" : "1000",
"SuitableForPets" : "Yes",
"SuitableForSmoking" : "No",
"SuitableForchildren" : "Yes"
},
"BasicInfo" : {
"AirportCode" : "ATL",
"City" : "Mableton",
"Country" : "United States",
"ModelPlane" : "Learjet",
"NameOfListing" : "Roomy 7 Seater Plane",
"NumberOfGuests" : "0",
"Owner" : "DjElZ1UT7jOOS23kjGwIr6lP5u22",
"PrimarilySetupForGuests" : "Yes",
"State" : "Georgia",
"Street" : "1368 Foxhall Place",
"ZipCode" : "30126"
},
"DetailedInformation" : {
"IdealGuests" : "Big groups",
"ImageRef" : {
"-L035nMB96ykxHolPYSE" : "B3488600-EFB8-4A7B-B60B-B595E4AFB52E"
},
"SummaryOfPlane" : "Give us a brief description of your plane..."
}
}
},
"-L0CiG0yGBvC0bHRjz3W" : {
"LevelOfQuestionnaireCompletion" : false,
"ListingData" : true
}
}
我只对" BasicInfo"下的信息感兴趣。并创建了一个类来获取与用户相关的信息:
Listing.kt
data class Listing(
val ModelPlane: String = "",
val AirportCode: String = "",
val City: String = "",
val State: String = "")
相关代码如下:
ListingAdapter.kt
class ListingAdapter(listingModel: Class<Listing>, modelLayout: Int, viewHolderClass: Class<ListingHolder>, dbRef: DatabaseReference) :
FirebaseRecyclerAdapter<Listing, ListingHolder>(listingModel, modelLayout, viewHolderClass, dbRef) {
override fun populateViewHolder(viewHolder: ListingHolder?, model: Listing?, position: Int) {
viewHolder!!.planeModel.text = model!!.ModelPlane
viewHolder.airportCode.text = model.ModelPlane
}
}
ListingHolder.kt
class ListingHolder(v: View) : RecyclerView.ViewHolder(v) {
var planeModel: TextView = v.listing_plane_model
var airportCode: TextView = v.listing_airport_code
}
将所有内容粘合在一起的代码:
for (listing in keyList) {
val listingReference = mDatabaseReference
.getReference("Listings/$listing/ListingData/BasicInfo")
listingReference.keepSynced(true)
search_results.adapter = ListingAdapter(Listing::class.java, listings_view_layout, ListingHolder::class.java, listingReference)
}
我非常确定在创建我的适配器类时我错过了(或搞砸了)但是我不完全确定是什么,因为我看过的所有指南都是似乎遵循了我已经完成的相同模式。 ($listing
对应于为数据库中的每个商家信息指定的唯一ID,例如"-L0CiG0yGBvC0bHRjz3W"
)
答案 0 :(得分:0)
对于之后来的任何人 - 我设法通过在我的数据库引用中向上移动一级(从@Html.DropDownListFor(m => m.Item1, Model.PossibleItems)
@Html.DropDownListFor(m => m.Item2, Model.PossibleItems)
转到Listings/$listing/ListingData/BasicInfo
)来解决此问题,似乎Firebase足够智能以获取特定内容我自己需要的信息(而不需要我具体告诉它在哪里),现在一切正常!