简体   繁体   English

在循环中获取 LiveData 转换 android

[英]get LiveData Transformations in loop android

I am getting all categories from a different endpoint, different repository.我从不同的端点、不同的存储库获取所有类别。 That is giving me all categories.那是给我所有的类别。 Now i want to run this categories in loop and write observer to get all the items of each category.现在我想循环运行这些类别并编写观察者来获取每个类别的所有项目。 In other words - I want to get FeedItems of all categories one by one because i have to fire separate endpoint to each category.换句话说 - 我想一一获取所有类别的 FeedItem,因为我必须为每个类别触发单独的端点。 I have list of all categories.我有所有类别的列表。

I am able to get single category's items by writing below code, but do not know how to get multiple.我可以通过编写下面的代码来获取单个类别的项目,但不知道如何获取多个。

In viewModal i am doing below code for to single category Observer在 viewModal 中,我正在为单一类别观察者执行以下代码

  val categoryByIdItems: LiveData<Resource<List<FeedItem>>> = Transformations
        .switchMap(categoryByIdParam) { input ->
            input.ifExists { catId, shouldFetch ->
                feedRepository.loadCategoryByIdFeed(catId.toString(), shouldFetch)
            }
        }

if i use above code it will give me feedItems of one category by calling load function如果我使用上面的代码,它将通过调用加载 function 给我一个类别的 feedItems

fun load(categories:List<Category>,shouldFetch:Boolean = false){
    setCategoryByIdRequest(categories[0].id, shouldFetch)
}

I want to run setCategoryByIdRequest in loop based on categories, i can do that.我想根据类别在循环中运行 setCategoryByIdRequest,我可以这样做。 I am using this code to get first category items in fragment file我正在使用此代码获取片段文件中的第一类项目

viewModel.categoryByIdItems.observe(this, Observer { resource ->
})

if i run the loop for viewModel.categoryByIdItems.observe than layout adds in fragment but because of same observer all are same.如果我为viewModel.categoryByIdItems.observe运行循环而不是布局添加片段但由于相同的观察者都是相同的。

But i am not sure how can i get categoryByIdItems in loop as observe in fragment.但我不确定如何在片段中观察到循环中的 categoryByIdItems。 as categoryByIdItems i am defining in view-modal first and using that in fragment but i want that dynamic based on number of categories.作为 categoryByIdItems 我首先在视图模式中定义并在片段中使用它,但我希望基于类别数量的动态。

Sorry for my writing as i do not know how to explain more to tell you what i want.对不起我的写作,因为我不知道如何解释更多来告诉你我想要什么。 I am new with LiveData and Transformations.我是 LiveData 和转换的新手。 I am not using DB, using apis to get data.我没有使用 DB,使用 apis 来获取数据。 let me know if you want to find more code about repository, view modal or fragment, i'll provide you.如果您想找到更多关于存储库、查看模式或片段的代码,请告诉我,我会为您提供。

I would advise you to get all category in a list first from DB and then using kotlin filter to fetch by ID like this我建议您首先从 DB 获取列表中的所有类别,然后使用 kotlin 过滤器按 ID 获取,如下所示

DAO

@Query("SELECT * FROM categories_table") fun getAll(): LiveData<List> @Query("SELECT * FROM categories_table") fun getAll(): LiveData<List>

VIEWMODEL视图模型

private val _categories = DAO.getAll()

val categories : LiveData<Category>
   get() = _categories

ACTIVITY/ FRAGMENT活动/片段

viewModel.categories .observe(viewLifecycleOwner, Observer { it ->

if(it!=null){

//filter your data
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM