Remove List Items

   Remove List Items

    kotlin code:


package com.example.myapplication

import android.annotation.SuppressLint

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

import android.widget.*

class MainActivity : AppCompatActivity() {
var array: ArrayList<String> = ArrayList()
val description = arrayOf("Birth attendant", "Supervisor","Midwife", "Birth attendant", "Supervisor", "Midwife",
"Birth attendant", "Supervisor","Midwife", "Birth attendant", "Supervisor", "Midwife",
"Birth attendant", "Supervisor","Midwife", "Birth attendant", "Supervisor", "Midwife",
"Birth attendant", "Supervisor","Midwife", "Birth attendant", "Supervisor", "Midwife",
"Birth attendant", "Supervisor","Midwife", "Birth attendant", "Supervisor", "Midwife",)

@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// title=findViewById(R.id.title)

val listView = findViewById<ListView>(R.id.listView)
array.add("pradeep")
array.add("jsadsad")
array.add("pardeep")

array.add("jsadsad")

array.add("jsadsad")

array.add("jsadsad")

val myListAdapter = MyListAdapter(this, array, description)
listView.adapter = myListAdapter


}
}

     xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />





</LinearLayout>



MyAdapter


package com.example.myapplication

import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MyListAdapter(private val context: AppCompatActivity, private val title: ArrayList<String>, private val description: Array<String>)
: ArrayAdapter<String>(context, R.layout.custom_list, title) {

override fun getView(position: Int, view: View?, parent: ViewGroup): View {
val inflater = context.layoutInflater
val rowView = inflater.inflate(R.layout.custom_list, null, true)

val titleText = rowView.findViewById(R.id.title) as TextView
val imageView = rowView.findViewById(R.id.icon) as ImageView
val subtitleText = rowView.findViewById(R.id.description) as TextView
titleText.text = title[position]
titleText.setOnClickListener {
title.removeAt(position)
notifyDataSetChanged()

}
subtitleText.text = description[position]

return rowView
}
}


row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="horizontal">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
android:textStyle="bold"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />

<TextView
android:id="@+id/decprtion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="description"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:textSize="16sp"/>
</LinearLayout>

<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher" />

</LinearLayout>

</LinearLayout>


Comments

Popular posts from this blog

Spinner select validation

Open the Camera Pic The Image send to Server Base64