android recyclerview在成功改装呼叫后显示为空

cu6pst1q  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(373)

我正试图建立一个 RecyclerView 使用来自改装的响应。但是,我遇到了一个问题,当我的日志显示我的数据库中有数据时,我的回收器变成了空的白色 ArrayList 从网络响应(在熟悉kotlin之前,我还不想设置mvvm。)
循环转换器

class PlaylistRecyclerAdapter (private val playListNames: Array<String>) :
    RecyclerView.Adapter<PlaylistRecyclerAdapter.PlayListViewHolder>() {

    // Describes an item view and its place within the RecyclerView
    class PlayListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val playlistTextView: TextView = itemView.findViewById(R.id.playlist_name_text)

        fun bind(word: String) {
            playlistTextView.text = word
        }
    }

    // Returns a new ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayListViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.playlist_name_item, parent, false)

        return PlayListViewHolder(view)
    }

    // Returns size of data list
    override fun getItemCount(): Int {
        return playListNames.size
    }

    // Displays data at a certain position
    override fun onBindViewHolder(holder: PlayListViewHolder, position: Int) {
        holder.bind(playListNames[position])
    }

}

mainactivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
        val templist = getPlaylistItems()
        //Log.d("RESPONSE", "onCreate: "+templist.get(0).toString())

        recyclerView.adapter = PlaylistRecyclerAdapter(templist.toTypedArray())
        recyclerView.adapter?.notifyDataSetChanged()
    }

    private fun getPlaylistItems(): ArrayList<String> {
        var playlisttitles = ArrayList<String>()

        var BASE_URL = "https://flicastdemo.s3.amazonaws.com/jwplayer/"
        val retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val service = retrofit.create(HomeWebService::class.java)
        val call = service.getHomeContent()

        var home = HomeRoot()

        call.enqueue(object : Callback<HomeRoot> {
            override fun onResponse(call: Call<HomeRoot>, response: Response<HomeRoot>) {
                if (response.code() == 200) {
                    home = response.body()

                    if(!home.equals(null))
                    {
                        //Log.e("HOME", "val: " + home.toString())
                        for (i in 0 until home.content.size){

                            val BASE_URL = "https://cdn.jwplayer.com/v2/"
                            val retrofit = Retrofit.Builder()
                                .baseUrl(BASE_URL)
                                .addConverterFactory(GsonConverterFactory.create())
                                .build()

                            val service = retrofit.create(PlaylistWebService::class.java)
                            val call = service.getPlayListItem(home.content.get(i).playlistId) //"1QhdrFVq"

                            call.enqueue(object : Callback<PlaylistRoot> {
                                override fun onResponse(call: Call<PlaylistRoot>, response: Response<PlaylistRoot>) {
                                    if (response.code() == 200) {
                                        var playlistinfo : PlaylistRoot = response.body();
                                        playlisttitles.add(playlistinfo.title)
                                        Log.e("PlaylistTitle!", "onResponseTitle: "+playlistinfo.title)
                                    }
                                }
                                override fun onFailure(call: Call<PlaylistRoot>, t: Throwable) {
                                    Log.d("NO!NO!NO!", "onResponse: "+"NO!")
                                    playlisttitles.add("No Playlist")
                                }
                            })
                        }
                    }
                }
            }
            override fun onFailure(call: Call<HomeRoot>, t: Throwable) {
                Log.d("NO!NO!NO!", "onResponse: "+"NO!")
            }
        })

        return playlisttitles
    }
}
dxxyhpgq

dxxyhpgq1#

改型在后台线程中返回数据,因此 onResponse() 与ui是异步的,也就是说,在数据进入之前需要一些时间;因此 getPlaylistItems() 方法将在更新数据之前返回。因此,它返回一个空列表 val templist = getPlaylistItems() .
要解决这个问题,您可以创建一个侦听器接口,或者只构建 RecyclerViewonResponse 回拨:

override fun onResponse(call: Call<PlaylistRoot>, response: Response<PlaylistRoot>) {
    if (response.code() == 200) {
        var playlistinfo : PlaylistRoot = response.body();
        playlisttitles.add(playlistinfo.title)
        Log.e("PlaylistTitle!", "onResponseTitle: "+playlistinfo.title)

        recyclerView.adapter = PlaylistRecyclerAdapter(playlisttitles.toTypedArray())
        recyclerView.adapter?.notifyDataSetChanged()            
    }
}

相关问题