androiddatabasekotlinlistviewonlongclicklistener

Kotlin Simple Listview Remove Row


I want to delete the long clicked line in my dynamic list I created. But I couldn't write adapter. I would be glad if you can help. If interested, I can edit the more necessary code blocks.It is not necessary to delete it with a long click. I am open to other suggestions.

Customers.kt

  var compnameList=ArrayList<String>()
lateinit var  toggle: ActionBarDrawerToggle

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_customers)
    supportActionBar!!.setTitle("Müşteriler")




    getDataParse()



    listView.setOnItemClickListener { adapterView, view, i, l ->

        val intent=Intent(applicationContext,CustomersDetails::class.java)
        intent.putExtra("Name",compnameList[i])
        startActivity(intent)
    }

    listView.setOnItemLongClickListener { adapterView, view, i, l ->

       //İt'snot define


    }









}


fun getDataParse(){

    val search=findViewById<SearchView>(R.id.searchView)
    val listView=findViewById<ListView>(R.id.listView)

    val arrayAdapter=ArrayAdapter(this,android.R.layout.simple_list_item_1,compnameList)
    listView.adapter=arrayAdapter
    registerForContextMenu(listView)


   

Solution

  • Here is the code edited with a basic list view adapter, if this is what you need:

    var compnameList=ArrayList<String>()
    lateinit var  toggle: ActionBarDrawerToggle
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_customers)
    
        supportActionBar!!.setTitle("Müşteriler")
    
        getDataParse()
    
        findViewById<ListView>(<your-list-view-id>).adapter = ListViewAdapter()
    }
    
    inner class ListViewAdapter : BaseAdapter() {
    
        override fun getCount(): Int = compnameList.size
    
        override fun getItem(position: Int): Any = compnameList[position]
    
        override fun getItemId(position: Int): Long = position.toLong()
    
        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val view = convertView ?: LayoutInflater.from(parent?.context).inflate(android.R.layout.simple_list_item_1, parent, false)
    
            ViewHolder(view).bind(compnameList[position])
    
            return view
        }
    
        inner class ViewHolder(itemView: View) {
    
            val text1 = itemView.findViewById<LinearLayout>(R.id.text1)
    
            fun bind(compname: String) {
                text1.text = compname.nome
    
                itemView.setOnClickListener {
                    val intent=Intent(this, CustomersDetails::class.java)
                    intent.putExtra("Name", compname)
                    startActivity(intent)
                }
    
                itemView.setOnLongClickListener {
    
                }
            }
        }
    }