Я пытаюсь закодировать приложение для Android в Android Studio 3.5.1. Я определил функцию с именем addItem, которая добавляет входные данные типа Item (реализованный класс) в список Items, которые отображаются внутри recylerview. В классе ScreenKitchen эта функция работает нормально, но если я хочу использовать эту функцию в классе ScreenAdd, она просто не работает.
package com.example.kitchenwatcher2
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.screen_kitchen.*
class ScreenKitchen: AppCompatActivity() {
private var s = Item("s", 1, 0, 0)
private var b = Item("b", 1, 0, 0)
var item = mutableListOf(b)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.screen_kitchen)
run { addItem(s);println("Item s added") }
Add.setOnClickListener {
startActivity(Intent(this, ScreenAdd::class.java))
}
val viewkitchen = findViewById<RecyclerView>(R.id.view_kitchen)
viewkitchen.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
val adapter = ItemAdapter(item)
viewkitchen.setAdapter(adapter)
}
fun addItem(inp: Item?) {
if (inp != null) {
item.add(inp)
println("Item ${inp.title} added")
} else println("No item assigned")
}
}
рабочая часть
package com.example.kitchenwatcher2
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.screen_add.*
class ScreenAdd: AppCompatActivity() {
private fun toIntOr0(input:Any): Int {
input.toString().toIntOrNull()
return if (input is Int) input else 0
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.screen_add)
button.setOnClickListener{
val a: Int = toIntOr0(amount.text.toString())
val min : Int = toIntOr0(minamount.text.toString())
val max :Int = toIntOr0(maxamount.text.toString())
val i = Item(name.text.toString(), a, min, max)
run {ScreenKitchen().addItem(i)}
if (i in ScreenKitchen().item) {Toast.makeText(this,"done", Toast.LENGTH_LONG).show(); println("Found i in item")}
startActivity(Intent(this,ScreenKitchen::class.java))
}
}
}
не рабочая часть
package com.example.kitchenwatcher2
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class Item(val title: String, var amount: Int, val min: Int, val max: Int ) : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_listitem)
val add = findViewById<Button>(R.id.add)
val remove = findViewById<Button>(R.id.remove)
add.setOnClickListener{ amount + 1; Toast.makeText(this,"added 1", Toast.LENGTH_LONG).show();println("add clicked") }
remove.setOnClickListener{ amount - 1; if (amount < 0) {amount = 0}; Toast.makeText(this,"removed 1", Toast.LENGTH_LONG).show();println("remove clicked") }
}
}
класс элемента, используемый в коде
package com.example.kitchenwatcher2
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class ItemAdapter(inp_list: MutableList<Item>) : RecyclerView.Adapter<ItemAdapter.ViewHolder>(){
private val list = inp_list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.layout_listitem, parent, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item: Item = list[position]
holder.title.text = item.title
holder.amount.text = item.amount.toString()
println("assigned holder values")
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val title = itemView.findViewById(R.id.title) as TextView
val amount = itemView.findViewById(R.id.amount) as TextView
}
}
класс адаптера, используемый в коде