所以,我已经遵循了许多YouTube教程,以正确了解Android中的Retrofit以及如何将响应中的数据传递到我的适配器并显示列表,但现在没有一个对我有效,我不知道为什么。
在我的MainActivity中,我正在调用RetrofitInstance,因此向我的API发起GET请求并从中获取响应。当我调试我的代码并记录响应时,主体是正确可见的,因为它应该是,但是当我试图将该响应以列表的形式传递给适配器时,问题就发生了。
这是两个文件的代码
主活动代码-
class QuizMainActivity : AppCompatActivity() {
private lateinit var quizCategoryAdapter: QuizCategoryAdapter
private lateinit var binding: ActivityQuizMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityQuizMainBinding.inflate(layoutInflater)
setContentView(binding.root)
hideactionbar()
setupRecyclerView()
lifecycleScope.launchWhenCreated {
val response = try{
RetrofitInstance.api.getQuizCategories()
}catch (e:IOException){
Log.d("QuizCatTag", "IOException"+e)
return@launchWhenCreated
}catch (e: HttpException){
Log.d("QuizCatTag", "HTTPException"+e)
return@launchWhenCreated
}
if(response.isSuccessful && response.body()!=null){
setupRecyclerView()
quizCategoryAdapter.quizCategoryList = response.body()!!.category_detail //place where I am sending my response list to my adapter.
Log.d("quizCategoryList", quizCategoryAdapter.quizCategoryList.toString() )
}else{
Log.d("TASG", "Response not successful")
}
}
}
private fun hideactionbar() {
supportActionBar?.hide()
}
private fun setupRecyclerView() = binding.quizCategoryRv.apply {
quizCategoryAdapter = QuizCategoryAdapter(this@QuizMainActivity)
adapter = quizCategoryAdapter
layoutManager = LinearLayoutManager(this@QuizMainActivity)
}
}
字符串
适配器代码-(我也在适配器中使用DiffUtils来处理数据的变化)
class QuizCategoryAdapter(
private val context: Context
) : RecyclerView.Adapter<QuizCategoryAdapter.QuizCategoryHolder>() {
private val diffCallBack = object: DiffUtil.ItemCallback<CategoryDetail>(){
override fun areItemsTheSame(oldItem: CategoryDetail, newItem: CategoryDetail): Boolean {
return oldItem.category_name == newItem.category_name
}
override fun areContentsTheSame(oldItem: CategoryDetail, newItem: CategoryDetail): Boolean {
return oldItem == newItem
}
}
private val differ = AsyncListDiffer(this, diffCallBack)
var quizCategoryList : List<CategoryDetail>
get() = differ.currentList
set(value) {differ.submitList(value)}
val textList = quizCategoryList.chunked(3)
class QuizCategoryHolder(binding: QuizCategoryItemBinding) : RecyclerView.ViewHolder(binding.root){
val listTextV = listOf(binding.qciTv1, binding.qciTv2, binding.qciTv3)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuizCategoryAdapter.QuizCategoryHolder {
return QuizCategoryHolder(QuizCategoryItemBinding.inflate(LayoutInflater.from(context), parent,false))
}
override fun getItemCount(): Int {
return textList.size
}
override fun onBindViewHolder(holder: QuizCategoryHolder, position: Int) {
for (i in 0 until textList[position].size){
if(textList[position][i].category_name==""){
holder.listTextV[i].isVisible = false
}else{
holder.listTextV[i].text = textList[position][i].category_name
}
}
holder.itemView.setOnClickListener{
val intent = Intent(context, QuizQuestionActivity::class.java)
context.startActivity(intent)
}
}
}
型
我试图通过Retrofit将从API获取的列表显示到RecyclerView适配器中。但不幸的是,当我将列表传递给我的Adapter类时,它仍然显示为null/empty,并且不显示任何内容。
话虽如此,当我试图将列表作为参数传递给class时,它会显示列表。在我看来,我不认为这是正确的方式。
请引导我,让我知道你的想法。实现这一点的最正确的方法是什么?
2条答案
按热度按时间qltillow1#
在适配器中设置列表后,您缺少notifyDataSetChanged()。
字符串
在设置从API调用中获得的列表后,应调用。
mfuanj7w2#
所以我调试和Log.Cat我的代码中的一切,终于找到了错误在哪里-
基本上,这份名单没有任何问题。该列表将按预期的方式发送到适配器。但在我的适配器类中,我正在分块列表,如您所见
字符串
现在,这一行和textList变成了null,因为当我最终从API获取列表时,这个列表没有更新。因此导致不显示任何内容的错误。
型
因此,为了修复这个错误,我只是在显示之前将onBindViewHolder方法中的列表分块,并编辑了我的getItemCount()方法。
Adapter类的更新代码看起来像这样-
型