LoginSignup
roronyan
@roronyan

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ListViewで表示したリストをタップすることで選択状態を保持したい(複数選択)

解決したいこと

ListViewで表示したリストをタップすることで選択状態を保持したい(複数選択)

■使用言語
Kotlin

■実現したいことリスト…
・タップしたリストを選択状態として保持したい
・リストは複数選択できるようにしたい
・タップしたリストは再度押すことで未選択状態に戻るようにしたい
・選択状態になっているリストは色付けして目視で判断できるようにしたい
・削除ボタンを押すことで選択したリストを削除したい

■前提
・ListViewを使ってアプリ立ち上げ時に8つのリストを表示しています。
・リスト追加用のボタンを設け、後からリストを追加できるように実装しています。
 (今回の質問とは関係無い処理です)
・リスト削除用のボタンを用意しています。押すと確認のダイアログが表示されます。
 これは用意しているだけで削除の処理は未実装です…。

発生している問題・エラー

実現したいことが多くて大変申し訳ないのですが、最初の取っ掛かりとなるListViewを選択状態にする方法がネット上には見当たらず、行き詰まっています…。

該当するソースコード

MainActivity.kt

package com.example.listview_sample

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import com.example.listview_sample.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val lv = binding.lv
        val btnAdd = binding.btnAdd
        val deletebtn = binding.deleteBtn

        // データを用意
        val data = mutableListOf(
            "a", "b", "c", "d",
            "e", "f", "g", "h"
        )

        //アダプターに入れてListViewをセット
        val adapter = ArrayAdapter(
            this,
            android.R.layout.simple_list_item_1,
            data
        )
        lv.adapter = adapter

        //追加ボタン押すことでダイアログを表示する
        btnAdd.setOnClickListener{
            //EditText
            val et = EditText(this)
            AlertDialog.Builder(this)
                .setTitle("項目の追加")
                .setView(et)
                .setPositiveButton("追加"){ _, _ ->
                    //add()でアダプターに追加する
                    val adlist = et.text.toString()
                    adapter.add(adlist)
                }
                .setNegativeButton("取消",null)
                .show()
        }

        //ファイル削除時のボタン押下アクション
        deletebtn.setOnClickListener{
            AlertDialog.Builder(this)
                .setTitle("項目を削除しますか?")
                //Yesを押したらremoveで削除、未実装部なのでnull
                .setPositiveButton("Yes",null)
                .setNegativeButton("No",null)
                .show()
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

    </ListView>

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="10dp"
        android:text="追加"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/deleteBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_baseline_delete_24" />
</androidx.constraintlayout.widget.ConstraintLayout>

補足情報

■AndroidStudio ver
Android Studio Chipmunk | 2021.2.1 Patch 2

■ターゲットOS
9.0~13.0 (13.0でコード動かしてます)

ListView_sample.png

削除ボタン押下時.png

0

No Answers yet.

Your answer might help someone💌