我无法将listview
与edittext
进行比较。我想比较一下
tampil
等于list1
,将出现“数据终止”,如果不相等,则将出现“数据终止”。
有解决方案吗?
我尝试将tampil更改为String(),将比较更改为 运算符“ ==”,但不起作用。
--- MainActivity.kt ---
package com.indraalfendipurba.sortir1
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.view.View
import android.widget.*
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
lateinit var input: EditText
lateinit var button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
input = findViewById(R.id.input)
button = findViewById(R.id.button)
val list = mutableListOf<String>("satu", "dua")
val data = ArrayAdapter(applicationContext, android.R.layout.simple_list_item_1, list)
lv.adapter = data
val list1 = list.iterator()//untuk mengecek kondisi nilai atw element pada list
val tampil = input.text
var found = false
button.setOnClickListener {
for (n in list1) {
if (n.equals(tampil)) {
found = true
break
}
}
if (found) {
Toast.makeText(applicationContext, "data tersedia", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(applicationContext, "data tidak tersedia ", Toast.LENGTH_LONG).show()
}
}
}
}
----- activity_main.xml -----
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"[enter image description here][1]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="352dp"
android:layout_height="215dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="16dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" app:layout_constraintHorizontal_bias="0.0" android:id="@+id/lv"
android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="34dp"
app:layout_constraintBottom_toTopOf="@+id/input"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/button" android:layout_marginEnd="16dp" android:layout_marginRight="16dp"
android:layout_marginLeft="16dp" android:layout_marginStart="16dp" android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="84dp" app:layout_constraintTop_toBottomOf="@+id/input"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:id="@+id/input" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/lv"
app:layout_constraintBottom_toTopOf="@+id/button" android:hint="masukan data"
android:layout_marginBottom="16dp"/>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
您需要添加:
found = false
位于按钮的侦听器的开头。
还有为什么要遍历作为迭代器对象的list1
(为什么需要它)?
遍历list
:
button.setOnClickListener {
found = false
for (n in list) {
if (n.equals(tampil)) {
found = true
break
}
}
if (found) {
Toast.makeText(applicationContext, "data tersedia", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(applicationContext, "data tidak tersedia ", Toast.LENGTH_LONG).show()
}
}