我如何合并/映射两个具有通用键/字段的JSON响应并在Recyclerview Android中显示并使用一些Kotlin操作员进行搜索?

发布于 2025-02-07 17:55:23 字数 1599 浏览 3 评论 0 原文

做以下最好的方法是什么:

我有一个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
    }

What is the best way to do the following:

I have one json like:

  {
    "restaurants":[
    {
    "id":1,
    "name":"Chinese Food",
    "neighborhood":"New york",
    "photograph":"abc.jpg",
  "cuisine_type":"Chinese"
    }, ..]

and second json response is

 {  "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",
    
    }
    ]
}, ...],...}]}

I want to show all restaurants from first json response in recyclerview and along with it I want to show their respective menus also in the same card using the second response, where restaurant id is common in both the responses.

I have thought of couple of ways of doing it, like adding categoies and menu items in the first response/data class itself and then populating the UI from adapter or maybe using the common key in both the response as hashmap key and storing entire category from second response for each restaurant as value in hashmap and then in onbindViewholder finding category and then menu-items from second response object.

But this all seems little unclean, and i was thinking if there was a simpler way to achieve this using some kotlin operators. Can anyone suggest anything simpler and better?

P.S
I want to implement search filter also later, so I am looking for a solution in which i can search also between both the responses, like restaurant name from first response and menuitems from second response, and then my recyclerview will show the searched items.
Thanks in advance.

what I have done write now (using common key as Hashmap key) looks like this:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2025-02-14 17:55:23

您可以使用

.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 provided

map { } that that returns a list of items, where each item is transformed or modified from what you provided in the map function

You can find more Kotlin collection operations from the following links

https://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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文