I need your help! (Kotlin & Android)


#1

In my Android application, when I add the numbers in the array list that I dynamically created, I am adding up. However, I can not remove the count in the row that I delete. The example screen image is available on the link.

MainActivity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.ListView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

var arrayList: ArrayList<Values> = ArrayList()
var m_listItems = ArrayList<Int>()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    var listView = findViewById<ListView>(R.id.list_view)
    listView.adapter = MyArrayAdapter(applicationContext, arrayList)
    val m_adapter = ArrayAdapter<Int>(applicationContext, android.R.layout.simple_list_item_1, m_listItems)

    add_button.setOnClickListener(View.OnClickListener {

        var input_number = edit_number.getText().toString().toInt()
        var input_text = edit_text.getText().toString()

        arrayList.add(Values(input_text, input_number))
        m_listItems.add(input_number)

        var sum = 0
        for (element in m_listItems)
           sum += element

        total_tv.setText("TOTAL: $sum")
       m_adapter.notifyDataSetChanged()
    })
}

}

MyArrayAdapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*

class MyArrayAdapter (var screen: Context, var value: ArrayList) : BaseAdapter() {

private  class ViewHolder(row: View?) {

    var tv_text:TextView
    var tv_number:TextView
    var delete_button: Button

    init {
        this.tv_text =row?.findViewById(R.id.tv_text) as TextView
        this.tv_number =row?.findViewById(R.id.tv_number) as TextView
        this.delete_button =row?.findViewById(R.id.delete_button) as Button
    }
}
override fun getView(position: Int, view_view: View?, viewGroup: ViewGroup?): View {

    var view:View?
    var viewHolder:ViewHolder

    if(view_view == null){

        var place =LayoutInflater.from(screen)
        view = place.inflate(R.layout.account_list, viewGroup,false)
        viewHolder =ViewHolder(view)
        view.tag=viewHolder


    }
    else {

        view=view_view
        viewHolder =view.tag as ViewHolder
    }

    var value:Values =getItem(position) as Values
    viewHolder.tv_text.text = value.text
    viewHolder.tv_number.text = value.number.toString()


    view!!.findViewById<Button>(R.id.delete_button).setOnClickListener(View.OnClickListener {


        removeItem(position)


    })

    return view as View
}

private fun removeItem(position: Int) {



    value.removeAt(position)
    notifyDataSetChanged()

}

override fun getItem(position: Int): Any {

    return value.get(position)
}

override fun getItemId(position: Int): Long {

    return position.toLong()
}

override fun getCount(): Int {
    return value.count()
}

}

Values Class

data class Values (var text:String, var number:Int)


#2

Don’t know if you got this working or not yet, but it seems to look like you haven’t updated the actual text when you remove. You only set it when you press the add button.