I am working on one project where I am not able to get json response in my recyclerview. Following is my code. I tried to use debug pointer but I am not getting any information in logcat or debug tab.
class MainActivity : AppCompatActivity() {
private val TAG = "MainActivity"
private lateinit var binding: ActivityMainBinding
lateinit var viewModel: MainViewModel
private val retrofitService = RetrofitService.getInstance()
val adapter = MainAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
viewModel = ViewModelProvider(this, MyViewModelFactory(MainRepository(retrofitService))).get(MainViewModel::class.java)
binding.recyclerview.adapter = adapter
viewModel.movieList.observe(this, Observer {
Log.d(TAG, "onCreate: $it")
adapter.setMovieList(it)
})
viewModel.errorMessage.observe(this, Observer {
})
viewModel.getAllMovies()
}
}
Models
data class MobileProducts( val products: List )
data class Product( val image_url: String, val name: String, val price: String, val rating: Int )
Retrofit
interface RetrofitService {
@GET("/nancymadan/assignment/db")
fun getAllMovies() : Call<List<MobileProducts>>
companion object {
var retrofitService: RetrofitService? = null
fun getInstance() : RetrofitService {
if (retrofitService == null) {
val retrofit = Retrofit.Builder()
.baseUrl("https://my-json-server.typicode.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
retrofitService = retrofit.create(RetrofitService::class.java)
}
return retrofitService!!
}
}
}
Viewmodel
class MainViewModel constructor(private val repository: MainRepository) : ViewModel() {
val movieList = MutableLiveData<List<MobileProducts>>()
val errorMessage = MutableLiveData<String>()
fun getAllMovies() {
val response = repository.getAllMovies()
response.enqueue(object : Callback<List<MobileProducts>> {
override fun onResponse(call: Call<List<MobileProducts>>, response: Response<List<MobileProducts>>) {
movieList.postValue(response.body())
}
override fun onFailure(call: Call<List<MobileProducts>>, t: Throwable) {
errorMessage.postValue(t.message)
}
})
}
}
JSON RESPONSE
{
"products": [
{
"name": "test",
"price": "34999",
"image_url": "url",
"rating": 4
},
{
"name": "test2",
"price": "999",
"image_url": "url",
"rating": 4
},]}
CodePudding user response:
You have not set any LayoutManager for the RecyclerView, and RecyclerView will not work without LayoutManger. so you should add this to your code :
binding.recyclerview.setLayoutManager(LinearLayoutManager(this))
and it will work if your MainAdapter class and the data have no problems.
CodePudding user response:
First thing you set layout manager for Recyclerview.
binding.recyclerview.setLayoutManager(LinearLayoutManager(this))
If you set in XML then no need by programmatically.
After getting response of Viewmodel livedata when you observe list data.
You have set list in adapter but use notify. Use this.
adapter.notifyDataSetChange()
Or You can use DiffUtill for notifying data in adapter it's good way.