Help us understand the problem. What is going on with this article?

KotlinでArrayList

More than 3 years have passed since last update.

ArrayListのテスト

データを入力、削除をするアプリケーション
まずはコードから

Main.kt
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListAdapter
import android.widget.ListView
import android.widget.EditText

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val input = findViewById(R.id.input1) as Button
        val delete = findViewById(R.id.saku) as Button
        val simpleListView: ListView = findViewById(R.id.simpleListView) as ListView
        val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1)
        simpleListView.adapter = adapter as ListAdapter
        input.setOnClickListener {
            val edit = findViewById(R.id.editex) as EditText
            adapter.add(edit.text.toString())
        }
        delete.setOnClickListener {
            val edit = findViewById(R.id.editex) as EditText
            adapter.remove(edit.text.toString())
        }
    }
}

次にXML

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.nimani.listas.MainActivity">

    <ListView
        android:id="@+id/simpleListView"
        android:layout_width="wrap_content"
        android:layout_height="370dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.517"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/input1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:text="入力"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        tools:text="入力" />

    <EditText
        android:id="@+id/editex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.831" />

    <Button
        android:id="@+id/saku"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:text="削除"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

スクリーンショット (161).png

ListViewを使うためにはadapterが必要でadapterを使うことでListViewに追加や削除をすることができる

nimani76
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away