简体   繁体   English

如何在使用viewmodel时将动态参数传递给android中的api

[英]How to pass dynamic parameters to rest api in android when using viewmodel

I am fetching news details from News Api site. 我从新闻Api网站获取新闻详情。 I am using ViewModel architecture so that the device does not fetch the results when orientation changes. 我正在使用ViewModel架构,以便在方向更改时设备无法获取结果。

As per various turorials i am able to fetch the result to a recyclerview using retrofit and viewmodel buy giving static parameters as query to the rest api. 根据各种turorial,我可以使用retrofit和viewmodel购买静态参数作为查询到其余api,将结果提取到recyclerview。

private void loadTopHeadlines() {
        ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
        Call<TopHeadlinesResponse> response = apiInterface.getTopHeadlines("in", 20, 1,
                "api_key");
        response.enqueue(new Callback<TopHeadlinesResponse>() {
            @Override
            public void onResponse(Call<TopHeadlinesResponse> call, Response<TopHeadlinesResponse> response) {
                topHeadlinesResponse.setValue(response.body().getArticles());
            }

            @Override
            public void onFailure(Call<TopHeadlinesResponse> call, Throwable t) {

            }
        });
    }

As you can see in the ViewModel class that is created the method getTopHeadlines() uses static parameters. 正如您在创建的ViewModel类中看到的那样,方法getTopHeadlines()使用静态参数。 How do i change it to dynamic parameters. 如何将其更改为动态参数。

Static parameter 静态参数

Call<TopHeadlinesResponse> response = apiInterface.getTopHeadlines("in", 20, 1,
                "api_key");

Dynamic parameter 动态参数

Call<TopHeadlinesResponse> response = apiInterface.getTopHeadlines(dynamic, dynamic, dynamic,
                "api_key");

So basically your method should accept those parameters, eg: 所以基本上你的方法应该接受这些参数,例如:

private void loadHeadlines(String stringValue, int number, int otherNumer)
//...do some stuff
Call<TopHeadlinesResponse> response = apiInterface.getTopHeadlines(stringValue, number, otherNumber, "api_key");
//rest stays the same
}

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

相关问题 在Android中使用Dagger2时如何使用AssistedInject将动态值作为参数传递给ViewModel - How to use AssistedInject to pass dynamic value as a parameter to ViewModel when using Dagger2 in Android viewmodel Android 中的动态参数 - Dynamic parameters in viewmodel Android 如何使用 Factory 将自定义参数传递给 ViewModel? - How to pass custom parameters to a ViewModel using Factory? 如何将多个 API 方法的参数传递给 ViewModel - How to pass parameters of more than one API Methods to ViewModel 使用 Hilt 进行依赖注入时如何将运行时参数传递给 ViewModel 的构造函数? - How to pass runtime parameters to a ViewModel's constructor when using Hilt for dependency injection? 如何以 MVVM 模式将参数值传递给 ViewModel? - How to pass parameters values to the ViewModel in MVVM pattern? Android Wearable API:如何传递动态列表? - Android Wearable API: how to pass a dynamic List? 如何在Android中使用Dagger2将字符串传递给ViewModel / Repository类? - How to pass a String to a ViewModel/Repository class using Dagger2 in Android? 使用Volley(Android)发送带有参数的REST Api的DELETE请求? - Send a DELETE Request using Volley (Android) to a REST Api with parameters? 关于如何在Android的Installation类中使用Rest API(service)传递ParseObject(Object)? - About how to pass the ParseObject(Object) using Rest API(service) in Installation class in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM