我如何合并/映射两个具有通用键/字段的JSON响应并在Recyclerview Android中显示并使用一些Kotlin操作员进行搜索?
做以下最好的方法是什么:
我有一个JSON:
{
"restaurants":[
{
"id":1,
"name":"Chinese Food",
"neighborhood":"New york",
"photograph":"abc.jpg",
"cuisine_type":"Chinese"
}, ..]
第二个JSON回应是
{ "menu":[
{
"restaurantId":1,
"allcategories":[
{
"id":"100",
"name":"Noodles",
"menu_items":[
{
"id":"800",
"name":"Hakka noodles",
"description":"Tasty hakka noodles",
"price":"350.00",
},
{
"id":"900",
"name":"Shezwan Hakka noodles",
"description":"Shezwan sauce spicy hakka noodles",
"price":"750.00",
}
]
}, ...],...}]}
我想向Recyclerview的第一张JSON回应中展示所有餐馆,而我也想在同一张卡中使用同一张菜单显示他们的菜单第二个响应,其中餐厅ID在两个响应中都是常见的。
我已经想到了几种方法,例如在第一个响应/数据类本身中添加categoies和菜单项,然后从适配器中填充UI,或者也许在hashmap键中使用common键,并从中存储整个类别每家餐厅作为Hashmap中的价值的第二个响应,然后在OnBindViewHolder中查找类别,然后从第二个响应对象中找到菜单项目。
但这似乎几乎没有干净,我在想使用一些Kotlin操作员有一种更简单的方法来实现这一目标。谁能提出更简单,更好的东西?
PS 我想稍后再实施搜索过滤器,因此我正在寻找一个解决方案,在此解决方案中,我也可以在两个响应之间进行搜索,例如First Response的餐厅名称和第二个响应中的Menuitems,然后我的RecyClerview将显示搜索的项目。 提前致谢。
我现在写的是写(将通用键作为哈希姆普键)如下所示:
override fun onBindViewHolder(holder: RestaurantAdapter.RestViewHolder, position: Int) {
getCategoryList(restList.get(position).id)?.forEach {
it?.let {
holder.binding.tvMenuItem.text= "${it?.menuItems}"
}
}
}
fun getCategoryList(restId:Int?):ArrayList<Category?>?{
return menuMap.get(restId)?.categories
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
.filter {&lt; filter predicate&gt; 根据您提供的条件返回项目的新列表
} 函数
您可以从以下链接中找到更多Kotlin
Collection
操作https:/ /Github.com/benct/kotlin-cheat-sheet
https://medium.com/mobile-app-development-pablication/kotlin-collection-functions-functions-cheat-sheat-975371a966c4b
=
You can use
.filter { <filter predicate> }
that returns a new list of items based on the condition you providedmap { }
that that returns a list of items, where each item is transformed or modified from what you provided in the map functionYou can find more Kotlin
collection
operations from the following linkshttps://github.com/benct/kotlin-cheat-sheet
https://medium.com/mobile-app-development-publication/kotlin-collection-functions-cheat-sheet-975371a96c4b
You can also perform a series of chained operations