요구사항
DTO
class ProductDto (
var name : String,
var amount : Int
) : java.io.Serializable
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initialize()
}
private fun initialize(){
binding.layoutInfo.setOnClickListener{
var intent = Intent(this@MainActivity,StuffActivity::class.java)
startActivity(intent)
}
}
}
activity_main.xml
ProductAdapter.kt
class ProductAdapter(private var productList : MutableList<ProductDto>) : BaseAdapter() {
fun setList(list : MutableList<ProductDto>){
productList = list
}
interface ItemClickListener{
fun onClick(view: View, position: Int)
}
lateinit var itemClickListener: ItemClickListener
override fun getCount(): Int = productList.size
override fun getItem(position: Int): Any {
return productList[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
@SuppressLint("ViewHolder", "SetTextI18n")
override fun getView(position: Int, view: View?, parent: ViewGroup): View {
val view : View = LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1,null)
val dto = productList[position]
view.findViewById<TextView>(android.R.id.text1).text = "물품 : ${dto.name} -> 수량 : ${dto.amount}"
view.setOnClickListener{
itemClickListener.onClick(view,position)
}
return view
}
}
stuffActivity
class StuffActivity : AppCompatActivity() {
private lateinit var binding: ActivityStuffBinding
private var list = mutableListOf<ProductDto>()
private lateinit var adapter: ProductAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityStuffBinding.inflate(layoutInflater)
setContentView(binding.root)
initialize()
}
fun initialize(){
binding.btnAdd.setOnClickListener{
var intent = Intent(this@StuffActivity,StuffEditActivity::class.java)
addLauncher.launch(intent)
}
addData()
adapter = ProductAdapter(list)
adapter.itemClickListener = object : ProductAdapter.ItemClickListener{
override fun onClick(view: View, position: Int) {
var intent = Intent(this@StuffActivity,StuffEditActivity::class.java).apply {
putExtra("position",position)
putExtra("dto",list[position])
}
editLauncher.launch(intent)
}
}
binding.list.adapter = adapter
}
fun addData(){
list.add(ProductDto("사과",10))
list.add(ProductDto("참외",11))
}
val addLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
if(it.resultCode == RESULT_OK){
Log.d(TAG,"return add")
val dto = it.data?.getSerializableExtra("dto") as ProductDto
list.add(dto)
adapter.setList(list)
adapter.notifyDataSetChanged()
}
}
val editLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
if(it.resultCode == RESULT_OK){
Log.d(TAG,"return edit")
val dto = it.data?.getSerializableExtra("dto") as ProductDto
val position = it.data?.getIntExtra("position",-1) ?: -1
if(position >= 0){
list[position] = dto
}
}else if(it.resultCode == 10){
Log.d(TAG,"return remove")
val position = it.data?.getIntExtra("position",-1) ?: -1
if(position >= 0){
list.removeAt(position)
}
}
adapter.setList(list)
adapter.notifyDataSetChanged()
}
activity_stuff
StuffEditActivity
class StuffEditActivity : AppCompatActivity() {
private lateinit var binding: ActivityStuffEditBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityStuffEditBinding.inflate(layoutInflater)
setContentView(binding.root)
initialize()
}
private fun initialize(){
var position = intent.getIntExtra("position",-1)
binding.apply {
btnSave.setOnClickListener{
var name = etProductName.text.toString()
var amount = etProductAmount.text.toString().toInt()
var dto = ProductDto(name,amount)
var intent = Intent().apply {
putExtra("dto",dto)
putExtra("position",position)
}
setResult(RESULT_OK,intent)
finish()
}
btnCancel.setOnClickListener{
finish()
}
}
if(position >= 0){ //수정
Log.d(TAG,"edit")
binding.apply {
var dto = intent.getSerializableExtra("dto") as ProductDto
// Log.d(TAG,"${dto.name}, ${dto.amount}")
binding.etProductName.setText(dto.name)
binding.etProductAmount.setText(dto.amount.toString())
btnDelete.setOnClickListener{
var intent = Intent().apply {
putExtra("position",position)
}
setResult(10,intent)
finish()
}
}
}else { //cancel
Log.d(TAG,"add")
binding.apply {
btnDelete.setOnClickListener{
finish()
}
}
}
}
}
activity_stuff_edit.xml