I am making a Kotlin project using a few functionalities
What the code does is as follows –
- Take a username from spinner
- 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
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
There seem top be multiple issues , I seperated your code into parts
SPINNER
LISTVIEW
GRIDVIEW INTENT
SECOND ACT
Manifest –
<activity android:name=".SecondActivity"/>
ALERT DIALOG
INVISIBLE
RADIO CHECKBOX
GRIDVIEW + NOTIFICATION