kotlin 无法显示回收程序视图

gpnt7bae  于 2023-08-06  发布在  Kotlin
关注(0)|答案(1)|浏览(132)

这是在Android Studio中的Kotlin中完成的。
每次我试图加载我的团队片段时,我都会得到一个空白屏幕,那里应该有一个回收器视图。
我相信这个错误可以在这个代码中找到。我得到了一个成功的响应,但无法将数据填充到回收器视图中。我相信我错误地将数据填充到了回收器视图中。

TeamsFragment.kt:


private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class TeamsFragment : Fragment() {
s
    private var param1: String? = null
    private var param2: String? = null
    private val retrofit by lazy {
        Retrofit.Builder()
            .baseUrl("https://api-football-v1.p.rapidapi.com/v3/")
            .addConverterFactory(MoshiConverterFactory.create())
            .build()
    }
    private val apiFootball by lazy { retrofit.create(APIFootball::class.java) }
    private val responseText: TextView by lazy { view?.findViewById<TextView>(R.id.api_response)!! }
    private val recyclerView: RecyclerView by lazy { view?.findViewById<RecyclerView>(R.id.recycler_view)!! }
    private val TeamsAdapter by lazy {
        TeamsAdapter(
            layoutInflater,
            GlideImageLoader(this),
            object : TeamsAdapter.OnClickListener {
                override fun onItemClick(TeamsData: MutableList<TeamResponse>)
                {
                }
            }
        )
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_teams, container, false)
        val recyclerView = view.findViewById<RecyclerView>(R.id.recycler_view)

        recyclerView.adapter = TeamsAdapter

        recyclerView.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)

        getTeamData()
        return view
    }
    fun getTeamData() {
            val call = apiFootball.getTeams(39, 2021)
            call.enqueue(object : Callback<ResponseData> {
                override fun onResponse(
                    call: Call<ResponseData>,
                    response: Response<ResponseData>
                ) {
                    if (response.isSuccessful) {
                        val responseData = response.body()
                        val teamList = responseData?.response ?: emptyList()

                        TeamsAdapter.setData(
                            teamList.map {
                                TeamModel(
                                    id = it.team.id,
                                    name = it.team.name,
                                    country = it.team.country,
                                    founded = it.team.founded,
                                    logo = it.team.logo,
                                    code = it.team.code,
                                    national = it.team.national,
                                )
                                
                            }
                        )
                    } else {
                        Log.e("API Error", "Response not successful.")
                    }
                }

                override fun onFailure(call: Call<ResponseData>, t: Throwable) {
                    Log.e("API Error", t.message.toString())                }



        })
    }
    companion object {
       
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            TeamsFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}



private fun <T> Response<T>.getInt(s: String) {

}

private fun <T> Response<T>.getString(s: String) {

}

字符串
我试着向ChatGPT和我的教授寻求解决方案。他问我,在调试器中查看这个之后,我的viewholder看起来是什么样子。Error He saw从那以后他就没有回应过。
第一个月

class TeamViewHolder(
    private val containerView: View,
    private val imageLoader: ImageLoader,
    private val onClickListener: OnClickListener
) : RecyclerView.ViewHolder(containerView) {
    private val teamNameView: TextView
            by lazy { containerView.findViewById(R.id.item_team_name) }
    private val teamCountryView: TextView
            by lazy { containerView.findViewById(R.id.item_team_country) }
    private val teamVenueView: TextView
            by lazy { containerView.findViewById(R.id.item_team_founded) }
    private val teamPhotoView: ImageView
            by lazy { containerView.findViewById(R.id.item_team_logo) }

    fun bindData(teamData: TeamResponse) {
        containerView.setOnClickListener { onClickListener.onClick(teamData) }
        imageLoader.loadImage(teamData.logo, teamPhotoView)
        teamNameView.text = teamData.name
        teamCountryView.text = teamData.country
    }

    interface OnClickListener {
        fun onClick(teamData: TeamResponse)
    }
}


下面是我的适配器:

TeamsAdapter.kt
    class TeamsAdapter(
        private val layoutInflater: LayoutInflater,
        private val imageLoader: ImageLoader,
        private val onClickListener: OnClickListener
        ) : RecyclerView.Adapter<TeamViewHolder>() {
            private val TeamsData = mutableListOf<TeamResponse>()
    
            fun setData(newTeamsData: List<TeamModel>) {
                TeamsData.clear()
                TeamsData.addAll(newTeamsData)
                notifyDataSetChanged()
            }
    
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TeamViewHolder {
                val view = layoutInflater.inflate(R.layout.item_team, parent, false)
                return TeamViewHolder(view, imageLoader, object : TeamViewHolder.OnClickListener {
                    override fun onClick(teamData: TeamResponse) = onClickListener.onItemClick(TeamsData)
                })
            }
    
    //    private fun showSelectionDialog(team: TeamUIModel) {
    //        AlertDialog.Builder(layoutInflater.context)
    //            .setTitle("Team Selected")
    //            .setMessage("You have selected ${team.name}")
    //            .setPositiveButton("OK") { _, _ -> }.show()
    //    }
    
            override fun getItemCount() = TeamsData.size
    
    
            override fun onBindViewHolder(holder: TeamViewHolder, position: Int) {
                holder.bindData(TeamsData[position])
            }
    
            interface OnClickListener {
                fun onItemClick(TeamsData: MutableList<TeamResponse>)
            }
        }
    
    private fun <E> List<E>.addAll(elements: List<TeamModel>) {
    
    }
'''

6ie5vjzr

6ie5vjzr1#

很难确切地说,因为它可能是多个东西,但这里有几件事你可以尝试:

  • 使用一个非常基本的片段,它只是有一个居中的“hello world”消息或其他东西,以确保您的片段在Activity中正确地膨胀。
  • 在xml布局预览中检查回收器视图。使用xmlns:tools="http://schemas.android.com/tools"查看其中包含项目的情况。它可以让你设置项目计数、项目布局、布局管理器等。
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
    tools:listitem="@layout/item"
    tools:itemCount="3"
    tools:orientation="horizontal"
    tools:scrollbars="horizontal" />

字符串

  • 使用调试器(特别是onCreateViewHolderTeamViewHolder中的调试器)确保数据被正确绑定。

相关问题