猿问

安卓从改造调用中获取实时数据

我正在尝试在我的Android项目上使用改造获取一些数据,并在视图模型上更新此数据以及我在LiveData上的活动。


这是我的服务类:


 class PaymentService {


        private var paymentMethodList = ArrayList<PaymentMethodModel>()

        private val paymentMethodListLiveData = MutableLiveData<List<PaymentMethodModel>>()


        init {

            paymentMethodListLiveData.value = paymentMethodList

        }


        fun fetchPaymentMethods() {

            val retrofit = Retrofit.Builder()

                .baseUrl(SERVICE_URL)

                .addConverterFactory(GsonConverterFactory.create())

                .build()


            val service = retrofit.create(PaymentClient::class.java)

            val jsonCall = service.getListOfPaymentMethods()

            jsonCall.enqueue(object : Callback<List<PaymentMethodModel>> {

                override fun onResponse(call: Call<List<PaymentMethodModel>>, response: Response<List<PaymentMethodModel>>) {

                    paymentMethodList = (response.body() as ArrayList<PaymentMethodModel>?)!!

                }


                override fun onFailure(call: Call<List<PaymentMethodModel>>, t: Throwable) {

                    //TODO

                }

            })

        }

以下是我试图听取列表中的更改的地方:


goToNextButton.setOnClickListener {


            paymentMethods = PaymentMethodSelectionViewModel().getAllPaymentMethods()

            paymentMethods!!.observe(viewLifecycleOwner, Observer {

                Log.e("", "")

            })

        }

问题是,到目前为止,我只第一次获得包含 0 个元素的列表,并且在进行 rest 调用并更新列表后,此观察者方法未被调用。


编辑


class PaymentRepository {


    private val paymentService = PaymentService()


    fun getPaymentMethods(): LiveData<List<PaymentMethodModel>> {

        paymentService.fetchPaymentMethods()

        return paymentService.getPaymentMethods()

    }


}


隔江千里
浏览 86回答 2
2回答

凤凰求蛊

将您的请求更改为视图模型class PaymentMethodSelectionViewModel: ViewModel() {&nbsp; &nbsp; //Data&nbsp; &nbsp; var paymentMethodList =&nbsp; MutableLiveData<List<PaymentMethodModel>>()&nbsp; &nbsp; fun getAllPayments(){&nbsp; &nbsp; &nbsp; &nbsp; val retrofit = Retrofit.Builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .baseUrl(SERVICE_URL)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addConverterFactory(GsonConverterFactory.create())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build()&nbsp; &nbsp; &nbsp; &nbsp; val service = retrofit.create(PaymentClient::class.java)&nbsp; &nbsp; &nbsp; &nbsp; val jsonCall = service.getListOfPaymentMethods()&nbsp; &nbsp; &nbsp; &nbsp; jsonCall.enqueue(object : Callback<List<PaymentMethodModel>> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override fun onResponse(call: Call<List<PaymentMethodModel>>, response: Response<List<PaymentMethodModel>>) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var data: List<PaymentMethodModel>&nbsp; = (response.body() as ArrayList<PaymentMethodModel>?)!!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;paymentMethodList.value=data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override fun onFailure(call: Call<List<PaymentMethodModel>>, t: Throwable) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //TODO&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }}在您的视图(活动)使用//loadpaymentMethodSelectionViewModel.getAllPayments();//Observers&nbsp;paymentMethodSelectionViewModel.paymentMethodList.observe(this,&nbsp; &nbsp;Observer { list ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// your code&nbsp; &nbsp; &nbsp; &nbsp;})我建议您将改装2与冠状图或RXJAVA2一起使用,请查看本教程https://medium.com/@amtechnovation/android-architecture-component-mvvm-part-1-a2e7cff07a76https://medium.com/@saquib3705/consuming-rest-api-using-retrofit-library-with-the-help-of-mvvm-dagger-livedata-and-rxjava2-in-67aebefe031d

陪伴而非守候

每次使用LiveData时,您都必须决定何时所有观察者都收到更新通知。您可以通过调用付款方法列表的过帐功能来执行此通知。这是在&nbsp;Java&nbsp;中使用实时数据的正确方法。在&nbsp;Kotlin&nbsp;中,我认为您必须在 onResponse 方法中添加类似如下的内容:paymentMethodListLiveData.value&nbsp;=&nbsp;paymentMethodList;以隐式调用观察函数中的 post 方法和触发器方法。希望这有帮助或给你一些提示。干杯
随时随地看视频慕课网APP

相关分类

Java
我要回答