回收者视图应该从提供给它的双数组创建按钮。回收站视图显示为空。不知道为什么

recycler view is supposed to create buttons from the double-array provided to it. the recycler view shows up empty instead. no idea why

至于我尝试解决问题的方法,创建了多个适配器,我认为稍微更改代码可能会解决问题,创建新的按钮布局。添加 toasts 以查看我正在谈论的双数组是否已被结果页面接收到。(它已经)。这些只是我记得的一些。

此外,我是 android 的新手。所以...

无论如何,我有一个图像可能更容易理解 - 从左下方标签的底部评论开始...

android studio screen shot

this is the new screenshot image

this is the code where the problem finally was solved

下面也添加了代码。

这就是问题开始的地方。我觉得...

结果页 class -

package com.kenetic.calculator_practice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.core.view.isEmpty
import androidx.recyclerview.widget.RecyclerView
import com.kenetic.calculator_practice.databinding.ActivityResultPageBinding
import com.kenetic.calculator_practice.databinding.ResultPageTestingBinding

class ResultPage : AppCompatActivity() {
    lateinit var resRecyclerView: RecyclerView
    lateinit var binding : ResultPageTestingBinding

    companion object{ var LIST = "list" }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_result_page)
        //resRecyclerView = findViewById(R.id.results_recyler_view)

        resRecyclerView = findViewById(R.id.test_recycler_view_one)
        val ListToSend = intent.extras?.getDoubleArray(LIST)!!.toList().toDoubleArray()
        resRecyclerView.adapter = ResultsAdapter(this,ListToSend)

        if (resRecyclerView.isEmpty()) {
            if (ListToSend.isEmpty()) {
                Toast.makeText(this, "list and view empty", Toast.LENGTH_SHORT).show()
            }
            else {
                Toast.makeText(this, ("only view empty "+ListToSend.size.toString()),Toast.LENGTH_SHORT).show()
            }
            //TODO - returning only view empty -_- That makes 0 sense.
            // any idea why the view is empty if the list isn't? cause I have been searching
            // the required conditions for a view to be empty and found almost nothing useful
        }
    }
}

结果适配器class-

package com.kenetic.calculator_practice

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView

class ResultsAdapter (
    private val context:Context,
    private val dataSet : DoubleArray) : RecyclerView.Adapter<ResultsAdapter.ResultViewHolder>() {

    class ResultViewHolder(view:View) : RecyclerView.ViewHolder(view){
        val textButton : Button = view.findViewById(R.id.result_button_test)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ResultViewHolder {
        val buttonView = LayoutInflater.from(parent.context).inflate(R.layout.test_button_layout,parent,false)
        makeToast()
        return ResultViewHolder(buttonView)
    }

    override fun onBindViewHolder(holder: ResultViewHolder, position: Int) {
        holder.textButton.text = dataSet[position].toString()
    }

    override fun getItemCount(): Int = dataSet.size

    fun makeToast(){
        Toast.makeText(context,dataSet[1].toString(),Toast.LENGTH_SHORT).show()
    }//TODO even this isn't getting called -_-
}

按钮布局-文件名-test_button_layout

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">
    <Button
        android:id="@+id/result_button_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</GridLayout>

最后,这是按钮应该可见的地方。 回收站视图 xml 文件 - 名称 - activity_result_page.xml

  <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/test_recycler_view_one"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

我们将不胜感激任何形式的帮助。提前致谢...

如果 Adapter 为空,您的 Adapter 将永远不会调用 onCreateViewHolder,这意味着 currentDataSet = dataSet 永远不会被调用。因此您的适配器永远不会包含任何要显示的数据。

所以你可以做两件事

删除 currentDataSet 并只使用 dataSet,无论如何你都用它初始化 Adapter

在您的 Adapter

中创建一个 setter
fun submitArray(doubleArray : DoubleArray){
   dataSet = doubleArray
   notifyDataSetChanged();
}

并在有新数据要显示时调用它。