I need help, I need that when you click for each cell of RecyclerView after clicking it and switching to a new activity using onclick on this activity, the name of the object and detailed information was appearing, if there are ideas how this can be done on Kotlin without a database(I managed to transfer the name using intent.putExtra)
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(),onClick {
private var recyclerView: RecyclerView? = null
private var item: ArrayList<Item>? = null
private var gridLayoutManager: GridLayoutManager? = null
private var adapter: Adapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById(R.id.item_list)
//количество grid элементов в строчке
gridLayoutManager =
GridLayoutManager(applicationContext, 2,
LinearLayoutManager.VERTICAL, false)
recyclerView?.layoutManager = gridLayoutManager
recyclerView?.setHasFixedSize(true)
item = ArrayList()
item = setItem()
//установка адаптера
adapter = Adapter(item!!,this)
recyclerView?.adapter = adapter
item_list.adapter=adapter
}
private fun setItem(): ArrayList<Item> {
//добавление элементов в RecyclerView
var arrayList: ArrayList<Item> = ArrayList()
arrayList.add(Item(R.drawable.koscmos, "A Latter"))
arrayList.add(Item(R.drawable.teatr, "B Latter"))
arrayList.add(Item(R.drawable.bridge, "C Latter"))
return arrayList
}
override fun onItemClick(position: Int) {
Toast.makeText(this,"id" +position,Toast.LENGTH_LONG).show()
//переход на новую активность с переносом названия в эту активность
val intent= Intent(this,secondAct::class.java)
intent.putExtra("name", item?.get(position)?.name)
startActivity(intent)
}
}
Adapter:
class Adapter(var arrayList: ArrayList<Item>, private val onClick: onClick) :
RecyclerView.Adapter<Holder>() {
//Установка холдера
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val viewHolder = LayoutInflater.from(parent.context)
.inflate(R.layout.item_list, parent, false)
return Holder(viewHolder)
}
override fun getItemCount(): Int {
return arrayList.size
}
override fun onBindViewHolder(holder: Holder, position: Int) {
//установка элементов для onClick
val item: Item = arrayList[position]
holder.icons.setImageResource(item.icons!!)
holder.titles.text = item.name
holder.icons.setOnClickListener {
onClick.onItemClick(position)
}
holder.titles.setOnClickListener {
onClick.onItemClick(position)
}
}
}
class Item : Serializable {}
now You can pass the object through Intent :
override fun onItemClick(position: Int) {
val intent= Intent(this,secondAct::class.java)
intent.putExtra("item", item?.get(position))
startActivity(intent)
}
then in secondAct
val item = intent.extras.get("item") as Item