首页 > 解决方案 > 列表项不会出现在屏幕上,而是打印在控制台上

问题描述

我正在尝试使用 Jsoup 解析网站。当我运行应用程序时,解析的项目进入控制台,我的意思是 println 语句,但项目列表没有进入片段的 recyclerview,只是出现一个空白屏幕。代码部分很长,我在这里粘贴整个片段(带有recyclerview)和适配器代码。任何帮助表示赞赏。

适配器类:

class ExampleAdapter : RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>() {
//    ExampleAdapter(private val exampleList: List<Books>)
    private val exampleList = mutableListOf<Books>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.example_item, parent, false)
        return ExampleViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {    
//        val currentItem = exampleList[position]
//        holder.imageView.setImageResource(currentItem.imageResource)
//        holder.textView1.text = currentItem.title
//        holder.textView2.text = currentItem.description
//        holder.textView3.text = currentItem.additionalInfo
        holder.bind(exampleList[position])
    }
    override fun getItemCount(): Int = exampleList.size

    class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.row_img
        val textView1: TextView = itemView.row_tv_title
        val textView2: TextView = itemView.row_tv_description
        val textView3: TextView = itemView.row_tv_additional_info

        fun bind(books: Books) {
            textView1.text = books.title
            textView2.text = books.description
            Picasso.with(itemView.context)
                .load(books.imageResource)
                .transform(CropSquareTransformation())
                .into(imageView)
            textView3.text = books.additionalInfo
//        link = books.linkDetails
        }
    }

    //exampleList class içindeki val değerlerini silmeyince unresolved reference hatası veriyor:
    fun set(list: MutableList<Books>) {
        this.exampleList.clear()
        this.exampleList.addAll(list)
        notifyDataSetChanged()
    }

}


class CropSquareTransformation : Transformation {
    override fun transform(source: Bitmap): Bitmap {
        val size = Math.min(source.width, source.height)
        val x = (source.width - size) / 2
        val y = (source.height - size) / 2
        val result = Bitmap.createBitmap(source, x, y, size, size)
        if (result != source) {
            source.recycle()
        }
        return result
    }

    override fun key(): String {
        return "square()"
    }

片段类:

class ListBooksFragment : Fragment() {

    private lateinit var adapter: ExampleAdapter
    private val url ="https://openlibrary.org/search?q=men&mode=everything"
    private val exampleList = mutableListOf<Books>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        recycler_view.layoutManager = LinearLayoutManager(this.context)
        adapter = ExampleAdapter()
        recycler_view.adapter = ExampleAdapter()

        GlobalScope.launch {
            getData()
        }

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_list_books, container, false)
    }

    companion object {

        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            ListBooksFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }



    private fun getData(){

        val list = ArrayList<Books>()

        try {
            val document = Jsoup.connect(url).get()
            val elements = document.select("li.searchResultItem")

            for (i in 0 until elements.size) {
                val title = elements.select("div.resultTitle")
                    .select("a.results")
                    .eq(i)
                    .text()

                println("title = " + title.toString())

//                AUTHOR:
//                val author = elements.select("span.bookauthor")
//                    .select("a.results")
//                    .eq(i)
//                    .text()

                val description = elements.select("span.bookauthor")
                    .select("a.results")
                    .eq(i)
                    .text()

                println("description = " + description.toString())

                val linkImage = "https://www.tutorialspoint.com/images/QAicon.png"
//                    document.baseUri() +
//                    elements.select("div[class=bookcover]")
//                        .select("img")
//                        .eq(i)
//                        .attr("src")

                val additionalInfo = elements.select("span.resultPublisher")
                    .select("span.publishedYear")
                    .eq(i)
                    .text()

                println("additionalInfo = " + additionalInfo.toString())

                exampleList.add(Books(imageResource = R.drawable.ic_launcher_background, title, description, additionalInfo))

               //Below 2 lines are just for another approach
                val item = Books(imageResource = R.drawable.ic_launcher_background, title, description, additionalInfo)
                list += item

            }
            GlobalScope.launch(Dispatchers.Main) {
                adapter.set(exampleList)
            }
        } catch (e: IOException) {
            Log.e("TEST EXCEPTION:::: ", e.message.toString())
        }
    }
}

标签: androidkotlinparsingandroid-recyclerviewjsoup

解决方案


您正在将列表加载到instanceRecyclerViewonActivityCreated

adapter = ExampleAdapter()              // New instance of ExampleAdapter assigned to adapter property
recycler_view.adapter = ExampleAdapter()// New instance of ExampleAdapter assigned to RV

然后您将列表加载为

adapter.set(exampleList)   // This adapter is not associated with the RV

要解决此问题,请将上述行更改onActivityCreated

adapter = ExampleAdapter()
recycler_view.adapter = adapter

推荐阅读