JosephFC97
JosephFC97

Reputation: 1

How do I initialize a TableLayout in Kotlin?

I have another fragment that I want to launch to when clicking a button and in that fragment I want to add stuff to a table. The only problem is, I try to initialize a TableLayout variable but it's never initialized. Here's the .kt file

package com.example.test1

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TableLayout
import android.widget.TableRow
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import x.Thirty.MainActivity
import x.Thirty.R
import x.Thirty.Score

class Result : Fragment(R.layout.fragment1) {
    companion object {
        const val KEY_RES = "key_res"
    }
    private lateinit var mainAdapter: MainActivity
    private lateinit var backBtn: Button
    private var resScore: Int = 0
    private lateinit var scoreTable: TableLayout
    private val scores: MutableList<Int> = mutableListOf()
    private val calcs: MutableList<String> = mutableListOf()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment1, container, false)
        mainAdapter = activity as MainActivity
        resScore = mainAdapter.resultScore
        scoreTable = view.findViewById(R.id.scoreTable2)

        if (savedInstanceState != null) {
            resScore = savedInstanceState.getInt(KEY_RES, 0)
        } else {
            mainAdapter = activity as MainActivity
            resScore = mainAdapter.resultScore
        }

        //This is used to display the score calculated in MainActivity. A back button is also initialized below to go back to the main page.
        val textView: TextView = view.findViewById(R.id.text1)
        textView.text = "Congratulations! Your total score was: ${resScore}"

        backBtn = view.findViewById(R.id.back)
        backBtn.setOnClickListener {
            fragmentManager?.popBackStack()
            mainAdapter.resultScore = 0
        }

        return view
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt(KEY_RES, resScore)
    }

    fun updateFragTable(score: Score)
    {
        // Ensure scoreTable is properly initialized
        if (!::scoreTable.isInitialized) {
            Log.e("ResultFragment", "scoreTable is not initialized")
            return
        }
    }



    
    }
}

and here's the fragment1.xml file

<?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"
    android:background="#FFF"
    android:clickable="true"

    >


    <TextView
        android:id="@+id/text1"
        android:layout_width="362dp"
        android:layout_height="99dp"
        android:text="Congratulations! Your total score was: "
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.489"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.517" />

    <Button
        android:id = "@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Return"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text1" />

    <TableLayout
        android:id="@+id/scoreTable2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/text1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Round\n1"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Round 2"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Round 3"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Round 4"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Round 5"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Round 6"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Round 7"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Round 8"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Round 9"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Round 10"
                android:textAlignment="center" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/score1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:textAlignment="center" />

            <TextView
                android:id="@+id/score2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:textAlignment="center" />

            <TextView
                android:id="@+id/score3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:textAlignment="center" />

            <TextView
                android:id="@+id/score4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:textAlignment="center" />

            <TextView
                android:id="@+id/score5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:textAlignment="center" />

            <TextView
                android:id="@+id/score6"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:textAlignment="center" />

            <TextView
                android:id="@+id/score7"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:textAlignment="center" />

            <TextView
                android:id="@+id/score8"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:textAlignment="center" />

            <TextView
                android:id="@+id/score9"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:textAlignment="center" />

            <TextView
                android:id="@+id/score10"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:textAlignment="center" />

        </TableRow>

 

    </TableLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

Now obviously in the function updateFragTable, I will do more stuff, but for now I just want to initialize the table to then be able to do stuff along the line of val s = (scoreTable.getChildAt(1) as TableRow).getChildAt(score.round) as TextView s.text = "${score.totalScore}" But for some reason, I check the logs and everytime I get "scoreTable is not initialized" and this is evident by the table never being filled. What am I doing wrong here? Why won't it be initialized?

Upvotes: 0

Views: 35

Answers (0)

Related Questions