0

// I solved my problem thank you for all the help of this community. Answer is posted below

So firstly I know that kotlin android extensions are not supported anymore. I will change that later. First I want to make sure my app works and has what it needs then Ill converge over. I am somewhat new to android studio so there is a chance I missed something simple. I did Import button and toast.

My app works fine if I remove the button onclick listener so I assume it must be the culprit.

Main activity

package com.dispatch.tripsheet

import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.table_list_item.*

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


        recyclerViewMovieList.layoutManager = LinearLayoutManager(this)

        recyclerViewMovieList.adapter = TableViewAdapter(movieList)


        // get reference to button
      //  val button1 = findViewById(R.id.button1) as Button

//        button1.setOnClickListener {
//
//            Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
//          tvdone.setBackgroundResource(R.color.green)
//
//        }

//        // get reference to button
//     //   val button2 = findViewById(R.id.button2) as Button
//        // set on-click listener
//        button2.setOnClickListener {
//            // your code to perform when the user clicks on the button
//            Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
//          tvdone.setBackgroundResource(R.color.orange)
//
//        }


        val button1 = findViewById(R.id.button1) as Button
        button1.setOnClickListener {
            Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
            tvdone.setBackgroundResource(R.color.green)
        }
//        button2.setOnClickListener {
//            Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
//            tvdone.setBackgroundResource(R.color.orange)
//        }

//            colour_orange(View view) {
//                // set the color to relative layout
//                relativeLayout.setBackgroundResource(R.color.cool)
//            }
//        });

//        // onClick function for button 2
//        button2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                // set the color to relative layout
//                relativeLayout.setBackgroundResource(R.color.warm)
//            }


        val list : MutableList<String> = ArrayList()
        list.add("Deon")
        list.add("Leon")
        list.add("David")
        list.add("Dick")
        list.add("Jim")
        list.add("Harry")


        val adapter = ArrayAdapter( this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, list)
        val spinner: Spinner = findViewById(R.id.spnDriver)

        spinner.adapter = adapter

        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                val item :String = list[p2]

                Toast.makeText(this@MainActivity, "Driver $item selected", Toast.LENGTH_SHORT).show()
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {
                //empty
            }

            //insert code that activates data pull of tripsheet for driver=


        }

        limitDropDownHeight(spinner)


    }
    fun limitDropDownHeight(spinner: Spinner){



        val popup = Spinner::class.java.getDeclaredField( "mPopup")
        popup.isAccessible = true

        val popupWindow = popup.get(spinner)as ListPopupWindow

        popupWindow.height = (200 * resources.displayMetrics.density).toInt()



    }

     val movieList = ArrayList<MovieModel>().apply {


//         var x: Int = 10
//
//         for (i in 1..x) {
//             println(i)
//
//
//         }

//         var totweight: Int = 40
//         tvTotalweight.setText("Total Weight: " +totweight+ "kg")

        add(MovieModel(190617, 182832, "Jcorp", "378 Begonia 6th street",100))
        add(MovieModel(190617, 182833, "Honda", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Panado", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Gheiters", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "John", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(9190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(190617, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(10, 182832, "Everflu", "365 hellington rogmastreet",100))
        add(MovieModel(10, 182832, "2010", "260",100))
        add(MovieModel(10, 182832, "2010", "260",100))
        add(MovieModel(10, 182832, "2010", "260",100))
        add(MovieModel(10, 182832, "2010", "260",100))
        add(MovieModel(10, 182832, "2010", "260",100))
        add(MovieModel(10, 182832, "2010", "260",100))
        add(MovieModel(10, 182832, "2010", "260",100))


     //need to change from hardcoded text to input
         
     }

    fun weightsum(args: Array<String>) {
        var totweight: Int = 40
        var sum: Int = 0
        //  var input: String

//        for (Int sum : movieList)
//        {
//            System.out.println(sum);
//        }


        tvTotalweight.setText("Total Weight: " +totweight+ "kg")



    }


}



Full XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"

    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txtWOrder"
        android:layout_width="160dp"
        android:layout_height="match_parent"
        android:layout_gravity="top"

        android:background="@drawable/table_content_cell_bg"
        android:text="WOrder"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtDElNote"
        android:layout_width="190dp"

        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:background="@drawable/table_content_cell_bg"
        android:text="DElNote"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtCompany"
        android:layout_width="190dp"

        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:background="@drawable/table_content_cell_bg"
        android:text="Company"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtWeight"
        android:layout_width="190dp"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:background="@drawable/table_content_cell_bg"
        android:text="Weight"
        android:textSize="18sp" />

<!--    <TextView-->
<!--        android:id="@+id/txtAddress"-->
<!--        android:layout_width="550dp"-->
<!--        android:layout_height="match_parent"-->
<!--        android:layout_gravity="top"-->
<!--        android:background="@drawable/table_content_cell_bg"-->
<!--        android:text="Address"-->
<!--        android:textSize="18sp" />-->


    <!--        android:onClick="ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName))"-->

    <Button
        android:id="@+id/button1"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:background="@drawable/table_content_cell_bg"


        android:text="@string/button1"
        android:textSize="18sp" />




    <Button
        android:id="@+id/button2"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:background="@drawable/table_content_cell_bg"

        android:text="@string/button2"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tvdone"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:background="@drawable/table_content_cell_bg"
        android:text="" />


</LinearLayout>


This one is to fill my recyclerview. The one to follow is my activtymain.:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    tools:context=".MainActivity"
    android:stretchColumns="*">

    <TableRow
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="1dp"
        android:layout_weight="1"
        android:background="#FFFFFF">

        <TextView
            android:id="@+id/tvHeader"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Vulcan Steel Trip Sheet: ID NUM TRIPSHEET"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="#000000"
            android:textSize="24sp"
            android:textStyle="bold" />

        <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spnDriver"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/spinner_bg"
            android:contentDescription="@string/driver"
            android:spinnerMode="dropdown" />


    </TableRow>

<!--    <TableRow-->
<!--        android:layout_width="0dp"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:layout_margin="1dp"-->
<!--        android:layout_weight="1"-->
<!--        android:id="@+id/headerrow"-->

<!--        android:background="#ffffff">-->

<!--        <TextView-->
<!--        android:layout_width="0dp"-->
<!--        android:layout_weight="1"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:layout_column="0"-->
<!--        android:layout_margin="1dp"-->
<!--        android:background="#FFFFFF"-->
<!--        android:gravity="center"-->
<!--        android:text=" W Order "-->
<!--        android:textAppearance="?android:attr/textAppearanceLarge"-->
<!--        android:textStyle="bold" />-->

<!--    <TextView-->
<!--        android:layout_width="0dp"-->
<!--        android:layout_weight="1"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:layout_column="1"-->
<!--        android:layout_margin="1dp"-->
<!--        android:background="#FFFFFF"-->
<!--        android:gravity="center"-->
<!--        android:text="Del Note "-->
<!--        android:textAppearance="?android:attr/textAppearanceLarge"-->
<!--        android:textStyle="bold" />-->

<!--    <TextView-->
<!--        android:layout_width="0dp"-->
<!--        android:layout_weight="1"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:layout_column="2"-->
<!--        android:layout_margin="1dp"-->
<!--        android:background="#FFFFFF"-->
<!--        android:gravity="center"-->
<!--        android:text="Company"-->
<!--        android:textAppearance="?android:attr/textAppearanceLarge"-->
<!--        android:textStyle="bold" />-->

<!--    <TextView-->
<!--        android:layout_width="0dp"-->
<!--        android:layout_weight="4"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:layout_column="2"-->
<!--        android:layout_margin="1dp"-->
<!--        android:background="#FFFFFF"-->
<!--        android:gravity="center"-->
<!--        android:text="Address"-->
<!--        android:textAppearance="?android:attr/textAppearanceLarge"-->
<!--        android:textStyle="bold" />-->


<!--    <TextView-->
<!--        android:layout_width="0dp"-->
<!--        android:layout_weight="1"-->
<!--        android:layout_height="wrap_content"-->
<!--        android:layout_column="2"-->
<!--        android:layout_margin="1dp"-->
<!--        android:background="#FFFFFF"-->
<!--        android:gravity="center"-->
<!--        android:text="Weight"-->
<!--        android:textAppearance="?android:attr/textAppearanceLarge"-->
<!--        android:textStyle="bold" />-->

<!--    </TableRow>-->

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="570dp"
        android:layout_margin="1dp"

        android:background="#ffffff"
        >

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="570dp"
            android:layout_margin="0dp"



            >


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewMovieList"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:nestedScrollingEnabled="false"
                tools:listitem="@layout/table_list_item" />

        </HorizontalScrollView>


    </TableRow>








    <TableRow
        android:id="@+id/footerow"
        android:background="#ffffff"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/tvTotaldelv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:text="Total: x Delivery Notes"
            android:textColor="#000000"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvTotalweight"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Total weight: # xkg"
            android:textColor="#000000"
            android:textSize="15sp" />
    </TableRow>


</TableLayout>

I have removed the toast and the color change to see if that might be it. But it still crashed. So not sure what I am missing.

Error log: 
2022-03-10 13:42:37.876 20228-20228/com.dispatch.tripsheet E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.dispatch.tripsheet, PID: 20228
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dispatch.tripsheet/com.dispatch.tripsheet.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3835)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8633)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.dispatch.tripsheet.MainActivity.onCreate(MainActivity.kt:25)
        at android.app.Activity.performCreate(Activity.java:8207)
        at android.app.Activity.performCreate(Activity.java:8191)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3808)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:8633) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 

I am not sure what it means exactly. Just shows me it happens something with the button. But I do not know much more. Image to show the app before crash. So everything works until I try to add a clicklistener.

helpVulcan
  • 105
  • 9
  • Can you please share the crash log as well? Also, where do you add the `button1.setOnClickListener` ? – peshkira Mar 10 '22 at 09:57
  • learn how to read crash logs. it should tell you what's wrong – Ivo Mar 10 '22 at 10:24
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Ivo Mar 10 '22 at 10:26
  • if you are using tvdone is the image then use tvdone.setBackgroundResource(R.drawable.) If tvdone is a textview then use -tvdone.setTextColor(ContextCompat.getColor(context, R.color.)); or tvdone.setBackgroundResource(getApplicationContext().getResources().getColor(R.color.black)); – Sandesh Khutal Mar 10 '22 at 10:53
  • @peshkira The ```button1.setonCLickListener``` is in my MainActivity.kt. The only other place it is mentioned in my code is in the xml file where it was created. – helpVulcan Mar 10 '22 at 11:56
  • @IvoBeckers Can you please help me with the error log or direct me to where I can find a good source to learn from. – helpVulcan Mar 10 '22 at 11:56
  • @helpVulcan Make sure you are importing correct layout/xml file and used correct ID of View. – Jinal Patel Mar 10 '22 at 11:58
  • @Sandesh Currently it is set as a textview. All I need is for the background to change. I have also considered a button. Can you maybe recommend me which one will be best suited. This is not the reason for my crash, but it will help me once I get there. – helpVulcan Mar 10 '22 at 12:00
  • @JinalPatel I added a photo to show the app working. So I am calling the right layout thankyou. The correct ID of view. I don't understand what you mean with this. If you can elaborate please – helpVulcan Mar 10 '22 at 12:05
  • @helpVulcan are you accessing button1 directly from layout or you're using findViewById or anything else? and button1 is in same layout? – Jinal Patel Mar 10 '22 at 12:08
  • @JinalPatel I am using Table layout. I am calling it directly now. Previously I did use ''' val button1 = findViewById(R.id.button1) as Button''' but it did not create a difference . – helpVulcan Mar 10 '22 at 12:16
  • @helpVulcan Can you share your XML file? – Jinal Patel Mar 10 '22 at 12:19
  • @JinalPatel I added all the xml I use in layout. – helpVulcan Mar 10 '22 at 12:38
  • 1
    You need to initialize `button1` by `findViewById` or with binding library, otherwise it will be null and hence the crash. Also make sure you are calling `setContentView` in your `onCreate` of `MainActivty`. Can you share more code of your `MainActivity`? – Torima Mar 10 '22 at 12:41
  • @Torima Thank you for your input. I added all of my MainActivity at the head of the question. – helpVulcan Mar 10 '22 at 12:48
  • @helpVulcan I think button1 is placed inside recyclerview item layout so you can access it in onBindViewHolder of recyclerview's adapter. – Jinal Patel Mar 10 '22 at 12:50
  • @helpVulcan Does `R.layout.activity_main` contain `button1` in xml? I dont't see it. I think you are using wrong layout. That is why findViewById returns null – Torima Mar 10 '22 at 12:53
  • @JinalPatel You are right. It is located inside my recyclerview. Do you recommend that I put the code in my adapter for the recyclerview? – helpVulcan Mar 10 '22 at 12:55
  • @Torima I think you might be right. Button 1 is located in table_list_item.xml which is called in activity_main.xml – helpVulcan Mar 10 '22 at 12:58
  • Yes, put click listeners in the adapter. Preferably set them in onCreateViewHolder and not in onBindViewHolder, since onBindViewHolder is being called a lot – Torima Mar 10 '22 at 13:02
  • @Torima I just moved my buttons and textview file to activity main.xml and it worked. Thank you. But now If I can ask. How can I access it if it is within the other xml file becuase I need it to be aligned next to my recylerview. – helpVulcan Mar 10 '22 at 13:03

3 Answers3

2

@helpVulcan Here is the adapter code snippet which you can use and customise as per your requirement,

class MyAdapter(val list: ArrayList<String>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.table_list_item, parent, false))
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.apply {
            button1.setOnClickListener {
                Log.e("Clicked", "button1")
                //  Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    class MyViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        var button1 = view.findViewById<Button>(R.id.button1)
    }

}
Jinal Patel
  • 229
  • 2
  • 7
  • Thank you for your effort. So this is where I will use the code now to access my buttons. – helpVulcan Mar 10 '22 at 13:07
  • @helpVulcan Upvote if it found helpful to you – Jinal Patel Mar 10 '22 at 13:08
  • Thank you for your effort. I have changed my code to go in the adapter. Sadly it still throughs an error and a crash. – helpVulcan Mar 10 '22 at 13:23
  • 2022-03-10 15:22:41.508 28711-28711/com.dispatch.tripsheet E/AndroidRuntime: FATAL EXCEPTION: main Process: com.dispatch.tripsheet, PID: 28711 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.dispatch.tripsheet.TableViewAdapter.onBindViewHolder(TableViewAdapter.kt:34) at com.dispatch.tripsheet.TableViewAdapter.onBindViewHolder(TableViewAdapter.kt:13) Sadly it is still failing. – helpVulcan Mar 10 '22 at 13:24
  • Make sure you have passed correct layout id in onCreateViewHolder which contains button1 view @helpVulcan – Jinal Patel Mar 10 '22 at 13:36
1

It looks like your button was not found. How are you getting access to it and where? Remember that the button needs to exist first so you can get the reference to it. You can use either the findViewById method or view binding.

ouflak
  • 2,458
  • 10
  • 44
  • 49
0

Through the help of this post and more research I found a solution. I knew my problem was the creating/calling of the button. My button was not located in my main activity xml file but in another. So in my adapter is where I had to put the code.

package com.dispatch.tripsheet



import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.table_list_item.view.*

class TableViewAdapter(private val movieList: List<MovieModel>) : RecyclerView.Adapter<TableViewAdapter.RowViewHolder>() {



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


    }

    private fun setHeaderBg(view: View) {
        view.setBackgroundResource(R.drawable.table_header_cell_bg)
    }

    private fun setContentBg(view: View) {
        view.setBackgroundResource(R.drawable.table_content_cell_bg)
    }

    override fun onBindViewHolder(holder: RowViewHolder, position: Int) {
        val rowPos = holder.adapterPosition



        if (rowPos == 0) {
            // Header Cells. Main Headings appear here
            holder.itemView.apply {
                setHeaderBg(txtWOrder)
                setHeaderBg(txtDElNote)
                setHeaderBg(txtCompany)
              //  setHeaderBg(txtAddress)
                setHeaderBg(txtWeight)
                setHeaderBg(txtbutton1)
                setHeaderBg(txtbutton2)
                setHeaderBg(txttvdone)


                txtWOrder.text = "WOrder"
                txtDElNote.text = "DElNote"
                txtCompany.text = "Company"
               // txtAddress.text = "Address"
                txtWeight.text = "Weight"
                txtbutton1.text = "Delivered"
                txtbutton2.text = "Exception"
                txttvdone.text = ""
            }
        } else {
            val modal = movieList[rowPos - 1]

            holder.itemView.apply {
                setContentBg(txtWOrder)
                setContentBg(txtDElNote)
                setContentBg(txtCompany)
              //  setContentBg(txtAddress)
                setContentBg(txtWeight)
                setContentBg(txtbutton1)
                setContentBg(txtbutton2)
                setContentBg(txttvdone)

                txtWOrder.text = modal.WOrder.toString()
                txtDElNote.text = modal.DElNote.toString()
                txtCompany.text = modal.Company.toString()
              //  txtAddress.text = modal.Address.toString()
                txtWeight.text = modal.Weight.toString()
                txtbutton1.text = modal.Button1.toString()
                txtbutton2.text = modal.Button2.toString()
                txttvdone.text = modal.tvdone.toString()
            }
        }

        holder.apply {
            txtbutton1.setOnClickListener {
                Log.e("Clicked", "Successful delivery")
                txttvdone.setBackgroundResource(R.color.green)
            }
            txtbutton2.setOnClickListener {
                Log.e("Clicked", "Exception on delivery")
                txttvdone.setBackgroundResource(R.color.orange)
            }
        }

    }

    override fun getItemCount(): Int { return movieList.size + 1 // one more to add header row
        }

     class RowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){


         val txttvdone:TextView = itemView.findViewById<TextView>(R.id.txttvdone)
         val txtbutton1:Button = itemView.findViewById<Button>(R.id.txtbutton1)
         val txtbutton2:Button = itemView.findViewById<Button>(R.id.txtbutton2)
     }

    class MyViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        var txtbutton1 = view.findViewById<Button>(R.id.txtbutton1)
        val txtbutton2:Button = itemView.findViewById<Button>(R.id.txtbutton2)
        var txttvdone = view.findViewById<TextView>(R.id.txttvdone)
    }
}
Gregoire Ducharme
  • 1,095
  • 12
  • 24
helpVulcan
  • 105
  • 9