Home > Mobile >  recycler view is supposed to create buttons from the double-array provided to it. the recycler view
recycler view is supposed to create buttons from the double-array provided to it. the recycler view

Time:11-24

as for what i have tried to solve the problem, created multiple adapters with slight code changes i thought might fix the problem, created new button layouts. added toasts to see if the double array i was talking about has even been received by the results page.(it has).these are just some I remember.

Also, I'm a newbie to android. so...

Anyways, I have an image which might be easier to understand - start from the bottom left tab's bottom comments...

android studio screen shot

this is the new screenshot image

this is the code where the problem finally was solved

code added below too.

This is where the problem starts. I think...

results page 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
        }
    }
}

Results adapter 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 -_-
}

button layout - file name - 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>

And finally, this is where the buttons are supposed to be visible. Recycler view xml file - name - 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>

any form of help would be much appriciated. thanks in advance...

CodePudding user response:

Your Adapter will never call onCreateViewHolder if it is empty, meaning currentDataSet = dataSet will never get called. Thus your adapter will never contain any data to display.

So you can do two things

remove currentDataSet and just use dataSet, your initializing the Adapter with it anyways.

OR

create a setter inside your Adapter

fun submitArray(doubleArray : DoubleArray){
   dataSet = doubleArray
   notifyDataSetChanged();
}

and call it when you have new data to display.

  • Related