This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Thursday, April 2, 2020

how to remove selected list item in kotlin

how to remove selected list item in kotlin 

In this Article today learn how to remove the selected items from listview in kotlin. android studio provides a listview this listview add list item and remove list item so following code how to remove selected item from listview in kotlin. follows the source code. 

 

how to clear listview in kotlin:

list item view, grid view, theme, style, design, data binding, room database, volley network library, kotlin studio syntax, java syntax, notification, navigation drawer, wifi, Bluetooth, system permissions, storage, media, seek bar, progress bar, web view,, transition, toolbar, google map, screen, jetpack, work manager, kotlin, fragment, audio manager, activity, action bar and many more.

 

lv.setOnItemLongClickListener(object:AdapterView.OnItemLongClickListener() {

  fun onItemLongClick(parent:AdapterView<*>, view:View, position:Int, id:Long):Boolean {

    builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title)

    //Setting message manually and performing action on button click

    builder.setMessage("Do you want to Delete List ?")

    .setCancelable(false)

    .setPositiveButton("Yes", object:DialogInterface.OnClickListener() {

      fun onClick(dialog:DialogInterface, id:Int) {

        fruits_list.remove(0)

        arrayAdapter.notifyDataSetChanged()

        Toast.makeText(this@MainActivity, "Removed Fruit", Toast.LENGTH_SHORT).show()

      }

    })

    .setNegativeButton("No", object:DialogInterface.OnClickListener() {

      fun onClick(dialog:DialogInterface, id:Int) {

        // Action for 'NO' Button

        dialog.cancel()


      }

    })


    //Creating dialog box

    val alert = builder.create()

    //Setting the title manually

    alert.setTitle("Delete List")

    alert.show()

    return true

  }

})



1.MainActivity.kt:

import android.content.DialogInterface

import android.os.Bundle

import android.app.Activity

import android.support.v7.app.AlertDialog

import android.view.View

import android.widget.AdapterView

import android.widget.ArrayAdapter

import android.widget.Button

import android.widget.ListView

import android.widget.Toast

import java.util.ArrayList

import java.util.Arrays

class MainActivity:Activity() {

  internal var builder:AlertDialog.Builder

  protected fun onCreate(savedInstanceState:Bundle) {

    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)

    // Get reference of widgets from XML layout

    val lv = findViewById(R.id.lv) as ListView

    // Initializing a new String Array

    val fruits = arrayOf<String>(

      "Apple",

      "Bacupari",

      "Beach Plum",

      "Black raspberry",

      "orange")

    // Create a List from String Array elements

    val fruits_list = ArrayList<String>(Arrays.asList<String>(*fruits))

    // Create an ArrayAdapter from List

    val arrayAdapter = ArrayAdapter<String>

    (this, android.R.layout.simple_list_item_1, fruits_list)

    // DataBind ListView with items from ArrayAdapter

    lv.setAdapter(arrayAdapter)

    builder = AlertDialog.Builder(this)

    lv.setOnItemLongClickListener(object:AdapterView.OnItemLongClickListener() {

      fun onItemLongClick(parent:AdapterView<*>, view:View, position:Int, id:Long):Boolean {

        builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title)

        //Setting message manually and performing action on button click

        builder.setMessage("Do you want to Delete List ?")

        .setCancelable(false)

        .setPositiveButton("Yes", object:DialogInterface.OnClickListener() {

          fun onClick(dialog:DialogInterface, id:Int) {

            fruits_list.removeAt(0)

            arrayAdapter.notifyDataSetChanged()

            Toast.makeText(this@MainActivity, "Removed Fruit", Toast.LENGTH_SHORT).show()

          }

        })

        .setNegativeButton("No", object:DialogInterface.OnClickListener() {

          fun onClick(dialog:DialogInterface, id:Int) {

            // Action for 'NO' Button

            dialog.cancel()

          }

        })

        //Creating dialog box

        val alert = builder.create()

        //Setting the title manually

        alert.setTitle("Delete List")

        alert.show()

        return true

      }

    })

  }

}



2.activity_main.xml:

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/rl"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="10dp"

    tools:context=".MainActivity"

    android:background="#d67f8d"

    >

        <ListView

        android:id="@+id/lv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

</RelativeLayout>

 

 

3.strings.xml:

<resources>

    <string name="app_name">Remove list</string>

    <string name="dialog_message">Delete item?</string>

    <string name="dialog_title">Delete List item</string>

</resources>

 

 

 

4.AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.akash.removelist">

 

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>










No comments:

Post a Comment