From everything I read it should work, however the following:
runOnUiThread {
recyclingView_Main.adapter = MainAdapter()
}
inside
fun fetchJson(){
println("attempting to fetch JSON")
val url = "https://api.drn1.com.au/station/allstations"
val request = Request.Builder().url(url).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
response.use {
if (!response.isSuccessful) throw IOException("Unexpected code $response")
for ((name, value) in response.headers) {
println("$name: $value")
}
val body = response.body!!.string()
val gson = GsonBuilder().create()
val stationfeed = gson.fromJson(body, StationsFeed::class.java)
runOnUiThread {
recyclingView_Main.adapter = MainAdapter()
}
println(body)
}
}
})
}
Is not working. Like the app loads, but the recyclingView_Main.adapter = MainAdapter()
seems to not run.
fullcode:
package com.example.drn1
import android.graphics.Color
import kotlinx.android.synthetic.main.activity_main.*
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.*
import java.io.IOException
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclingView_Main.layoutManager = LinearLayoutManager( this, LinearLayoutManager.HORIZONTAL, false)
// recyclingView_Main.adapter = MainAdapter() --- THIS DOES WORK WHEN IT IS HERE
fetchJson()
}
fun fetchJson(){
println("attempting to fetch JSON")
val url = "https://api.drn1.com.au/station/allstations"
val request = Request.Builder().url(url).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
response.use {
if (!response.isSuccessful) throw IOException("Unexpected code $response")
for ((name, value) in response.headers) {
println("$name: $value")
}
val body = response.body!!.string()
val gson = GsonBuilder().create()
val stationfeed = gson.fromJson(body, StationsFeed::class.java)
runOnUiThread {
recyclingView_Main.adapter = MainAdapter()
}
println(body)
}
}
})
}
}
class StationsFeed(val data: List<Data>)
class Data(val _id: String, val name: String, val imageurl: String)
As you can see from the code above when I put it in the onCreate it works fine.
So the issue seems to be sending it to the main thread.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclingView_Main"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>