In questo articolo vedremo come avere il controllo dei dati inseriti nella ListView tramite EditText.
Ecco come si presenta il file activity_main.xml
Ecco come si presenta il file MainActivity.kt
Alla riga 51 è stato aggiunto un controllo nel caso in cui la lista contenga l'elemento inserito nella EditText verrà mostrato un messaggio Toast altrimenti l'elemento verrà inserito nella ListView.
Ecco un video dimostrativo
Ecco come si presenta il file activity_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<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" | |
> | |
<EditText | |
android:id="@+id/editText" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:ems="20" | |
android:inputType="text" /> | |
<Button | |
android:id="@+id/bn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@id/editText" | |
android:text="Aggiungi elemento alla lista" /> | |
<ListView | |
android:id="@+id/list" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@id/bn" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dm.tutoriallistview6 | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.* | |
import java.util.* | |
//inizializzazione di un nuovo array di stringhe | |
private var fruits = arrayOf("") | |
//crea un elenco dagli elementi dell'Array di stringhe | |
private var fruits_list: MutableList<String> = ArrayList(Arrays.asList(*fruits)) | |
//crea un ArrayAdapter di tipo String | |
private var arrayAdapter: ArrayAdapter<String>? = null | |
//dichiarazione delle variabili associate agli oggetti | |
private lateinit var lv: ListView | |
private lateinit var btn: Button | |
private lateinit var edittext: EditText | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
//ottieni i riferimenti agli oggetti del layout | |
lv = findViewById(R.id.list) | |
btn = findViewById(R.id.bn) | |
edittext = findViewById(R.id.editText) | |
//crea un ArrayAdapter da lista impostando anche uno stile "simple_list_item_1" | |
arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, fruits_list) | |
//associazione della lista all'ArrayAdapter | |
lv.adapter = arrayAdapter | |
//rimuove la riga vuota | |
fruits_list.remove("") | |
} | |
override fun onResume() { | |
super.onResume() | |
//gestione del click sul pulsante | |
btn.setOnClickListener { | |
//verifica se l'EditText non è vuota | |
if(edittext.text.toString() != "") | |
{ | |
//verifica se l'elemento è già presente nella lista | |
if(fruits_list.contains(edittext.text.toString())) | |
{ | |
Toast.makeText(this, "Elemento già presente nella lista", Toast.LENGTH_SHORT).show() | |
} | |
else | |
{ | |
fruits_list.add(edittext.text.toString()) | |
} | |
} | |
else | |
{ | |
Toast.makeText(this, "Nessun elemento inserito", Toast.LENGTH_SHORT).show() | |
} | |
//permette di ordinare gli elementi all'interno della lista in ordine alfabetico italiano | |
fruits_list.sort() | |
fruits_list.forEach { print("$it ") } | |
/*notifyDataSetChanged () | |
Notifica che i dati sono stati modificati | |
e qualsiasi vista che riflette | |
il set di dati dovrebbe aggiornarsi.*/ | |
arrayAdapter!!.notifyDataSetChanged() | |
} | |
} | |
} |
Ecco un video dimostrativo
Download Project | ![]() |
Download file APK | ![]() |
Commenti
Posta un commento