skip to Main Content

I am making a Kotlin project using a few functionalities

What the code does is as follows –

  1. Take a username from spinner
  2. Takes the title of the budget from the radio buttons

It navigates to another page after displaying a toast message

Upon entering the required fields there are three buttons which do the following –

1.Upon clicking button1, there is a toast message displaying the total cost
2. Upon clicking button2, it displays an alert dialog stating whether the budget is greater than expenditure of vice-versa
3. Redirects to a new page with a listview and gridview

I am facing the following error –

None of the following functions can be called with the arguments supplied.
(Context, Int, Int, Array<(out) TypeVariable(T)!>)   where T = TypeVariable(T) for   constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, textViewResourceId: Int, objects: Array<(out) T!>) defined in android.widget.ArrayAdapter
(Context, Int, Int, (Mutable)List<TypeVariable(T)!>)   where T = TypeVariable(T) for   constructor ArrayAdapter<T : Any!>(context: Context, resource: Int, textViewResourceId: Int, objects: (Mutable)List<T!>) defined in android.widget.ArrayAdapter

Please help me out as I am new to Kotlin

Here is the code

MainActivity.kt

package com.example.b1_midsemquestion

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
import android.widget.AdapterView.OnItemSelectedListener

class MainActivity : AppCompatActivity() {
    private lateinit var selectedthing: String
    private lateinit var selectedItem: String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //spinner
        var spinner = findViewById<Spinner>(R.id.spinner)
        var spinnerArray = arrayOf("Ayush","Piyush","Random")
        val spinneradapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,spinnerArray)
        spinneradapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner.adapter = spinneradapter
        spinner.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                selectedItem = parent.getItemAtPosition(position).toString();

            }
            override fun onNothingSelected(parent: AdapterView<*>) {
            }
        }
        //spinner
        var radiogroup = findViewById<RadioGroup>(R.id.radiogrp)
        var submit = findViewById<Button>(R.id.button)
        submit.setOnClickListener {
            val selectedRadioButtonId: Int = radiogroup.checkedRadioButtonId
            if(selectedRadioButtonId != -1) {
                var selectedRadioButton = findViewById<RadioButton>(selectedRadioButtonId)
                selectedthing = selectedRadioButton.text.toString();

            }
            //toast message
            Toast.makeText(this@MainActivity, "$selectedthing is selected by $selectedItem",Toast.LENGTH_SHORT).show()
            //toast message
            //intent
            var intent = Intent(this@MainActivity,SecondActivity::class.java)
            intent.putExtra("key",selectedItem)
            intent.putExtra("key2",selectedthing)
            startActivity(intent)
            //intent

        }
        }



    }

SecondActivity.kt

package com.example.b1_midsemquestion



import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity


class SecondActivity : AppCompatActivity() {
    private lateinit var selectedthing:String;

    //alert dialog

    //alert dialog
    fun showAlert(context: Context, title: String, message: String) {
        val builder = AlertDialog.Builder(context)
        builder.setTitle(title)
        builder.setMessage(message)
        var textview: TextView = findViewById(R.id.textView4)
        textview.visibility = View.INVISIBLE
        builder.setPositiveButton("OK") { dialog, _ ->
            textview.text = "Ok Pressed";
            textview.visibility = View.VISIBLE
            dialog.dismiss()
        }
        val dialog = builder.create()
        dialog.show()
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.second_activity)
        var user = intent.getStringExtra("key")
        var expense = intent.getStringExtra("key2")
        var heading = findViewById<TextView>(R.id.textView2)
        heading.text = expense.toString();
        var budget = findViewById<EditText>(R.id.budget)
        var button1 = findViewById<Button>(R.id.button2)
        var button2 = findViewById<Button>(R.id.button3)
        var radiogrp2 = findViewById<RadioGroup>(R.id.radiogrp2)
        var button3 = findViewById<Button>(R.id.button4)
        var quantity = findViewById<EditText>(R.id.quantity)
        var price = findViewById<EditText>(R.id.price)
        var foodcost = findViewById<EditText>(R.id.foodcost)
        var transcost = findViewById<EditText>(R.id.transcost)
        var train = findViewById<CheckBox>(R.id.checkBox)
        var taxi = findViewById<CheckBox>(R.id.checkBox3)
        var flight = findViewById<CheckBox>(R.id.checkBox4)
        button1.setOnClickListener {
            var quantityval = quantity.text.toString();
            val quantityint = quantityval.toInt();
            var priceval = price.text.toString();
            val priceint = priceval.toInt();
            var transval = transcost.text.toString();
            val transint = transval.toInt();
            var foodval = foodcost.text.toString();
            val foodint = foodval.toInt();

            val totalcost = quantityint*priceint + foodint + transint;
            Toast.makeText(this@SecondActivity,"Total cost is $totalcost ", Toast.LENGTH_SHORT).show();
        }
        button2.setOnClickListener {
            var quantityval = quantity.text.toString();
            val quantityint = quantityval.toInt();
            var priceval = price.text.toString();
            val priceint = priceval.toInt();
            var transval = transcost.text.toString();
            val transint = transval.toInt();
            var foodval = foodcost.text.toString();
            val foodint = foodval.toInt();
            val totalcost = quantityint*priceint + foodint + transint;
            var budgetval = budget.text.toString();
            val budgetint = budgetval.toInt();
            if(totalcost > budgetint) {
                //alertdialog

                    showAlert(this,"Alert", "Total cost greater than budget")

                //alertdialog
            }
            else {

                    showAlert(this,"Alert", "Budget is greater than the total cost")

            }
        }
        button3.setOnClickListener {
            var intent2 = Intent(this@SecondActivity,ThirdActivity::class.java)
            var checkselected = "";
            var trainselected = "";
            var flightselected = "";
            var taxiselected = "";
            //checkboxes
            if(train.isChecked()) {
                trainselected = "train";
            }
            if(flight.isChecked()) {
                flightselected = "flight";
            }
            if(taxi.isChecked()) {
                taxiselected = "taxi";
            }
            //checkboxes
            intent2.putExtra("train",trainselected)
            //puting extra into intent
            intent2.putExtra("flight",flightselected)
            intent2.putExtra("taxi",taxiselected)

            //radiobutton
            val selectedRadioButtonId: Int = radiogrp2.checkedRadioButtonId
            if(selectedRadioButtonId != -1) {
                var selectedRadioButton = findViewById<RadioButton>(selectedRadioButtonId)
                selectedthing = selectedRadioButton.text.toString();

            }
            //radiobutton
            intent2.putExtra("key4", selectedthing)

            startActivity(intent2)

        }

    }
}

ThirdActivity.kt

package com.example.b1_midsemquestion



import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat


class ThirdActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.third_activity)
        var listview = findViewById<ListView>(R.id.listview)
        var train = intent.getStringExtra("train")
        var flight = intent.getStringExtra("flight")
        var taxi = intent.getStringExtra("taxi")
        var selectedthing = intent.getStringExtra("key4")

        var values = arrayOf(train,flight,taxi,selectedthing)

        //listview
        val listadapter = ArrayAdapter(
            this@ThirdActivity,
            android.R.layout.simple_list_item_1,
            values
        )
        listview.adapter = listadapter;
        listview.setOnItemClickListener { _, _, position, _ ->
            val selecteditem = values[position]
            Toast.makeText(this, "Item selected is $selecteditem", Toast.LENGTH_SHORT).show()
        }
        //listview


        //gridview
        var gridview = findViewById<GridView>(R.id.gridview)
        var items = arrayOf("ayush", "piyush", "random", "newone")
       var gridadapter = ArrayAdapter(this, R.layout.grid_item_layout, items, R.id.textView5)
        gridview.adapter = gridadapter;
        gridview.setOnItemClickListener { parent, view, position, id ->
            val selecteditem = items[position]
            val builder = NotificationCompat.Builder(this, "channel_id")
                .setSmallIcon(android.R.drawable.stat_notify_chat)
                .setContentTitle("Selected Item")
                .setContentText("You have selected $selecteditem")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)

            // Create a notification manager object
            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            // Check if the device is running Android Oreo or higher
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                // Create a notification channel
                val channel = NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT)
                channel.description = "Channel Description"
                // Register the channel with the system
                notificationManager.createNotificationChannel(channel)
            }
            // Issue the notification
            notificationManager.notify(1, builder.build())
        }
        //gridview





    }
}

2

Answers


  1. It seems like you have made a mistake on this line in the ThirdActivity.kt

    var gridadapter = ArrayAdapter(this, R.layout.grid_item_layout, items, R.id.textView5)

    The syntax should be

    var gridadapter = ArrayAdapter(this, R.layout.grid_item_layout, R.id.textView5, items)

    instead

    R.id.textView5 should come before items

    Login or Signup to reply.
  2. There seem top be multiple issues , I seperated your code into parts

    SPINNER

    package com.example.spinner
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.View
    import android.widget.AdapterView
    import android.widget.ArrayAdapter
    import android.widget.EditText
    import android.widget.Spinner
    import android.widget.TextView
    import android.widget.Toast
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            var options = arrayOf("verna","harrier","duster")
            val spinner = findViewById<Spinner>(R.id.spinner)
            val adapter = ArrayAdapter(this,android.R.layout.simple_spinner_item,options)
            var f1 = findViewById<EditText>(R.id.editTextText)
            var f2 = findViewById<TextView>(R.id.textView)
            spinner.adapter = adapter
            spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
                override fun onItemSelected(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ) {
                    var selectedItem = parent?.getItemAtPosition(position).toString()
                    Toast.makeText(this@MainActivity,selectedItem,Toast.LENGTH_SHORT).show()
                    f1.setText(selectedItem)
                    f2.setText(selectedItem)
                }
    
                override fun onNothingSelected(parent: AdapterView<*>?) {
                    TODO("Not yet implemented")
                }
            }
        }
    }
    

    LISTVIEW

    package com.example.listview
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.ArrayAdapter
    import android.widget.ListView
    import android.widget.TextView
    import android.widget.Toast
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            var cheezen = arrayOf("Hey","Yo","Wow")
            var ls = findViewById<ListView>(R.id.listview1)
            var f1 = findViewById<TextView>(R.id.textView)
            var adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,cheezen)
            ls.adapter = adapter
    
            ls.setOnItemClickListener { parent, view, position, id ->
                val selecteItem = cheezen[position]
                f1.setText(selecteItem)
            }
        }
    }
    

    GRIDVIEW INTENT

    package com.example.gridview2
    
    import android.content.Intent
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.ArrayAdapter
    import android.widget.GridView
    import android.widget.Toast
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            var gridView = findViewById<GridView>(R.id.gridView)
            var items = arrayOf("verna","harrier","duster","i10")
            var gridadapter = ArrayAdapter(this,R.layout.grid_item_layout,R.id.textView,items)
            gridView.adapter = gridadapter
    
            gridView.setOnItemClickListener { parent, view, position, id ->
                val selecteditem = items[position]
                val intent = Intent(this,SecondActivity::class.java)
                intent.putExtra("key",selecteditem)
                startActivity(intent)
            }
        }
    }
    

    SECOND ACT

    package com.example.gridview2
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.ArrayAdapter
    import android.widget.GridView
    import android.widget.TextView
    import android.widget.Toast
    
    class SecondActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.second_activity)
            var item = intent.getStringExtra("key")
            var f1 = findViewById<TextView>(R.id.textView2)
            f1.setText(item)
        }
    }
    

    Manifest – <activity android:name=".SecondActivity"/>


    ALERT DIALOG

    package com.example.alertdialog
    
    import android.content.Context
    import android.content.Intent
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Button
    import android.widget.EditText
    import androidx.appcompat.app.AlertDialog
    
    class MainActivity : AppCompatActivity() {
        fun showalert(context: Context,title:String,message: String){
            var alert = AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton("Move") { dialog, which ->
                    var intent = Intent(this,SecondActivity::class.java)
                    intent.putExtra("key",message)
                    startActivity(intent)
                }
                .setNegativeButton("Cancel"){ dialog , which ->
    
                }
                .create()
            alert.show()
        }
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            var f1 = findViewById<EditText>(R.id.editTextText)
            var btn = findViewById<Button>(R.id.button)
            btn.setOnClickListener {
                showalert(this,"Button pressed","you have written "+f1.text)
    
            }
        }
    }
    

    INVISIBLE

    package com.example.visibility
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.View
    import android.widget.Button
    import android.widget.ImageButton
    import android.widget.TextView
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            var btn = findViewById<ImageButton>(R.id.imageButton)
            var text = findViewById<TextView>(R.id.lol1)
            btn.setOnClickListener{
                if(text.visibility == View.VISIBLE){
                    text.visibility = View.INVISIBLE
                }
                else{
                    text.visibility = View.VISIBLE
                }
            }
        }
    }
    

    RADIO CHECKBOX

    package com.example.radiocheck
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.Button
    import android.widget.CheckBox
    import android.widget.RadioButton
    import android.widget.RadioGroup
    import android.widget.Toast
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            var radiogrp = findViewById<RadioGroup>(R.id.rad1)
            var btn = findViewById<Button>(R.id.button)
            var checkbox = findViewById<CheckBox>(R.id.checkBox)
            var checkbox2 = findViewById<CheckBox>(R.id.checkBox2)
            var btn2 = findViewById<Button>(R.id.button2)
            var selecteditem = findViewById<RadioButton>(R.id.radioButton3)
            radiogrp.setOnCheckedChangeListener { group, checkedId ->
                selecteditem = findViewById<RadioButton>(checkedId)
    
            }
            btn.setOnClickListener {
                if(checkbox2.isChecked && checkbox.isChecked){
                    Toast.makeText(this,"School ,College Done",Toast.LENGTH_SHORT).show()
                }
                else if(checkbox.isChecked){
                    Toast.makeText(this,"School Done",Toast.LENGTH_SHORT).show()
                }
                else if(checkbox2.isChecked){
                    Toast.makeText(this,"College Done",Toast.LENGTH_SHORT).show()
                }
    
    
            }
            btn2.setOnClickListener {
                Toast.makeText(this,selecteditem.text,Toast.LENGTH_SHORT).show()
            }
    
        }
    }
    

    GRIDVIEW + NOTIFICATION

    package com.example.gridview3
    
    import android.app.NotificationChannel
    import android.app.NotificationManager
    import android.content.Context
    import android.os.Build
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.ArrayAdapter
    import android.widget.GridView
    import androidx.core.app.NotificationCompat
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            var gridview = findViewById<GridView>(R.id.gv)
            var items = arrayOf("ayush", "piyush", "random", "newone")
            var gridadapter = ArrayAdapter(this, R.layout.grid_item_layout, R.id.textView, items)
            gridview.adapter = gridadapter;
            gridview.setOnItemClickListener { parent, view, position, id ->
                val selecteditem = items[position]
                val builder = NotificationCompat.Builder(this, "channel_id")
                    .setSmallIcon(android.R.drawable.stat_notify_chat)
                    .setContentTitle("Selected Item")
                    .setContentText("You have selected $selecteditem")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
    
                // Create a notification manager object
                val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    
                // Check if the device is running Android Oreo or higher
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    // Create a notification channel
                    val channel = NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT)
                    channel.description = "Channel Description"
                    // Register the channel with the system
                    notificationManager.createNotificationChannel(channel)
                }
                // Issue the notification
                notificationManager.notify(1, builder.build())
            }
    
        }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search