Kotlin 如何在rxjava中对网络api返回的数据进行分组
我从新闻api获得数据响应,我需要通过根据源对数据进行分组,在recycler视图中显示这些数据 基本上,我得到的是ArticalDto列表,我想将此列表转换为列表,因为我需要按源对数据进行分组,并在多个视图中的回收器视图中显示。我想使用GroupBy或Map来实现这一点,但没有得到确切的方法。下面是我为从网络获取数据而编写的代码Kotlin 如何在rxjava中对网络api返回的数据进行分组,kotlin,rx-java,Kotlin,Rx Java,我从新闻api获得数据响应,我需要通过根据源对数据进行分组,在recycler视图中显示这些数据 基本上,我得到的是ArticalDto列表,我想将此列表转换为列表,因为我需要按源对数据进行分组,并在多个视图中的回收器视图中显示。我想使用GroupBy或Map来实现这一点,但没有得到确切的方法。下面是我为从网络获取数据而编写的代码 class ContentListViewModel : BaseViewModel() { internal val result = MutableLiv
class ContentListViewModel : BaseViewModel() {
internal val result = MutableLiveData<Result<ArticlesDto>>()
/**
* Fetches news from sources.
* You do not need to understand this implementation in order to complete the assignment.
* All the updates (response) are posted to the [result] liveData.
*/
fun fetchNews(params: ContentParams = ContentParams()) {
NewsApiRepository(apiKey = apiKey).getEverything(
q = params.q,
sources = params.sources,
domains = params.domains,
language = params.language,
sortBy = params.sortBy,
pageSize = params.pageSize,
page = params.page
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.toFlowable()
.subscribe(
{ result.value = Result.success(it) },
{
Log.e("Error", it.cause?.message ?: "")
result.value = Result.failure(it)
}
).disposeOnClear()
}
}
class ContentListViewModel:BaseViewModel(){
内部val结果=MutableLiveData()
/**
*从消息来源获取新闻。
*您无需了解此实现即可完成任务。
*所有更新(响应)都会发布到[result]liveData。
*/
fun fetchNews(params:ContentParams=ContentParams()){
NewsApiRepository(apiKey=apiKey).getEverything(
q=参数q,
sources=参数sources,
域=参数域,
语言=参数语言,
sortBy=params.sortBy,
pageSize=参数pageSize,
page=params.page
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.toFlowable()
.订阅(
{result.value=result.success(it)},
{
Log.e(“错误”,it.cause?.message?:”)
result.value=result.failure(it)
}
).disposeOnClear()
}
}
有人能建议我如何使用rxjava实现这一点吗?我可以先将列表转换为Hashmap,然后再将其转换为list,但我想使用rxjava实现这一点。创建新的api响应pojo类。。 然后使用下面的代码
var categoryResult: MutableLiveData<List<CategoryModel>> = MutableLiveData()
使用viewModel更容易。我需要对从getEverything API返回的数据进行分组,以便调用GetCategine数据。
fun getCategoryData(storeId: String) {
if (ConnectivityUtils.isNetworkAvailable(getApplication())) {
showProgressDialog.postValue(true)
storeData = storeId
launch {
appApiInterface
.getCategories(storeId)
.with(schedulerProvider)
.subscribe(
{ result ->
showProgressDialog.postValue(false)
if (result.categories != null)
categoryResult.postValue(result.categories)
else
showNoData()
},
{ e ->
showProgressDialog.postValue(false)
interFragmentInterface?.showDialog(e.getErrorMessage(getApplication()))
}
)
}
} else {
interFragmentInterface!!.showDialog(getApplication<Application>().getString(R.string.error_message_network))
}
}
viewModel.categoryResult.observe(this, Observer { it ->
// here set api list of data into adapter class insert method.
// here set your adapter code.
})