簡體   English   中英

在listView中未選擇任何項目時吐司

[英]Toast when no item is selected in listView

大家好,我正在努力做到這一點:

         button.setOnClickListener {
            if (list1.choiceMode == android.widget.ListView.CHOICE_MODE_NONE){
                toast("You need to choose an item first")
            }
            else {
                val builder = AlertDialog.Builder(this)
                builder.setTitle("Alert")
                builder.setMessage("This service requires data?")
                builder.setPositiveButton("Yes", { dialogInterface: DialogInterface, i: Int ->
                    ListView.visibility = View.GONE
                    website.visibility = View.VISIBLE
                })
                builder.setNegativeButton("No", { dialogInterface: DialogInterface, i: Int -> })
                builder.show()
            }

        }

我試圖在用戶按下按鈕時顯示一條消息,但他尚未從list1中選擇任何項目。 如果一切正常,我希望它可以將listView可見性設置為不可見,並將網站可見性設置為可見。

按下按鈕時,我得到的只是吐司消息。

編輯:

    val nameofanimals = arrayOf("cat","dog","parrot")

internal lateinit var adapteranimals: ArrayAdapter<String>

這是我的清單:

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


    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:choiceMode="singleChoice"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:listSelector="@android:color/darker_gray"
        android:textSize="10dp"
        android:visibility="gone">


    </ListView>

我的適配器:

adapteranimals = ArrayAdapter(
        this@MainActivity,
        R.layout.list1layout,
        nameofanimals)
list1.adapter = adapteranimals

現在,當沒有選擇貓,狗或鸚鵡時,我需要顯示一條祝酒消息,並希望將Webviewer的可見性和listview布局設置為可見,並且當用戶實際選擇列表中的一個選項時消失

為了使您的項目可以在列表中選擇,您需要在列表視圖上調用兩種關鍵方法。

button.setOnClickListener之前添加這兩行

        list1.setItemsCanFocus(false);
        list1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

然后在您的onClickListener內部執行以下操作:

Object selectedObj = list1.getSelectedItem(); 

if (selectedObj == null) {
 showToast();
} else {
 doSomethingElse();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM