2021年3月6日星期六

RecyclerView doesn't show new data given to adapter after RestApi data request

This is my main activity, the initial data c1 c2 c3 is displayed correctly, but after the response comes back nothing changes, although the array after the response is 250 length, and the first member is also correct(it has all the fields as expected), am I missing something simple here? I thought maybe I should update on UI thread but that's not the problem. Country is a class I made, with 3 fields, name, nativeName and area.

private val BASE_URL = "https://restcountries.eu/rest/v2/"      override fun onCreate(savedInstanceState: Bundle?) {      super.onCreate(savedInstanceState)      setContentView(R.layout.activity_main)              var countriesArray: ArrayList<Country> = ArrayList()        var c1 = Country("111", "111n",111.0)      var c2 = Country("222", "222n",222.0)      var c3 = Country("333", "333n",333.0)            mainRecyclerView = findViewById(R.id.main_recycler_view)      linearLayoutManager = LinearLayoutManager(this)      mainRecyclerView.layoutManager = linearLayoutManager        adapter = RecyclerViewAdaper(countriesArray)      mainRecyclerView.adapter = adapter          countriesArray.add(c1)      countriesArray.add(c2)      countriesArray.add(c3)      adapter.notifyItemInserted(countriesArray.size-1)        Log.d(TAG, "array size 1 : " + countriesArray.size)        Log.d(TAG, "array: 1- " + countriesArray[0].name)        val retrofit = Retrofit.Builder()              .addConverterFactory(GsonConverterFactory.create())              .baseUrl(BASE_URL)              .build()        val mRestApiVal = retrofit.create(restApiInterface::class.java)        val mCall: Call<ArrayList<Country>> = mRestApiVal.getCounties()        mCall.enqueue(object : Callback<ArrayList<Country>> {            override fun onResponse(call: Call<ArrayList<Country>>, response: Response<ArrayList<Country>>) {              val mResponse: ArrayList<Country> = response.body()!!              countriesArray = mResponse                runOnUiThread { adapter.notifyItemInserted(countriesArray.size-1) }                adapter.notifyDataSetChanged()              adapter.notifyItemInserted(countriesArray.size-1)              Log.d(TAG, "onResponse: " + " response")              Log.d(TAG, "array size: " + countriesArray.size)              Log.d(TAG, "array: 2- " + countriesArray[0].name)              Log.d(TAG, "array: 2- " + countriesArray[0].nativeName)              Log.d(TAG, "array: 2- " + countriesArray[0].area)          }            override fun onFailure(call: Call<ArrayList<Country>>, t: Throwable) {              Log.e("Error ", t.message.toString())              Log.d(TAG, "onFailure: " + " onFailure")          }        })      }  
https://stackoverflow.com/questions/66512483/recyclerview-doesnt-show-new-data-given-to-adapter-after-restapi-data-request March 07, 2021 at 09:50AM

没有评论:

发表评论