我在 kotlin 中有一个数组列表,我想从中删除所有项目,将其保留为空数组以开始添加新的动态数据。我试过了,ArrayList.remove(index)
arrayList.drop(index)
但没有效果,
宣言:
var fromAutoCompleteArray: List<String> = ArrayList()
Run Code Online (Sandbox Code Playgroud)
这是我尝试的方法:
for (item in fromAutoCompleteArray){
fromAutoCompleteArray.remove(0)
}
Run Code Online (Sandbox Code Playgroud)
我正在使用addTextChangedListener
删除旧数据并根据用户输入添加新数据:
private fun settingToAutoComplete() {
val toAutoCompleteTextView: AutoCompleteTextView =
findViewById<AutoCompleteTextView>(R.id.toAutoCompleteText)
toAutoCompleteTextView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable?) {
doLocationSearch(toAutoCompleteTextView.text.toString(), 2)
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
toAutoCompleteTextView.postDelayed({
toAutoCompleteTextView.showDropDown()
}, 10)
}
})
val adapter = ArrayAdapter(this, android.R.layout.select_dialog_item, toAutoCompleteArray)
toAutoCompleteTextView.setAdapter(adapter)
toAutoCompleteTextView.postDelayed({
toAutoCompleteTextView.setText("")
toAutoCompleteTextView.showDropDown()
}, 10)
}
Run Code Online (Sandbox Code Playgroud)
这是添加数据的函数:
private fun doLocationSearch(keyword: String, fromTo: Number) {
val baseURL = "api.tomtom.com"
val versionNumber = 2
val apiKey = "******************"
val url =
"https://$baseURL/search/$versionNumber/search/$keyword.json?key=$apiKey"
val client = OkHttpClient()
val request = Request.Builder().url(url).build()
client.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: okhttp3.Response) {
val body = response.body?.string()
println("new response is : $body")
val gson = GsonBuilder().create()
val theFeed = gson.fromJson(body, TheFeed::class.java)
if (theFeed.results != null) {
for (item in theFeed.results) {
println("result address ${item.address.freeformAddress} ")
if (fromTo == 1) {
fromAutoCompleteArray = fromAutoCompleteArray + item.address.freeformAddress
println(fromAutoCompleteArray.size)
} else {
toAutoCompleteArray = toAutoCompleteArray + item.address.freeformAddress
}
}
} else {
println("No Locations found")
}
}
override fun onFailure(call: Call, e: IOException) {
println("Failed to get the data!!")
}
})
}
Run Code Online (Sandbox Code Playgroud)
正如你看到的,这条线会println(fromAutoCompleteArray.size)
告诉我它是否被删除了,而且它总是在增加。
此外,尝试在clear()
没有循环的情况下使用,但没有任何效果:
fromAutoCompleteArray.clear()
Tod*_*odd 22
List
Kotlin 中的类型是不可变的。如果要使列表更改,则需要将其声明为MutableList
.
我建议改变这一行:
var fromAutoCompleteArray: List<String> = ArrayList()
Run Code Online (Sandbox Code Playgroud)
对此:
val fromAutoCompleteArray: MutableList<String> = mutableListOf()
Run Code Online (Sandbox Code Playgroud)
然后你应该能够调用这些中的任何一个:
fromAutoCompleteArray.clear() // <--- Removes all elements
fromAutoCompleteArray.removeAt(0) // <--- Removes the first element
Run Code Online (Sandbox Code Playgroud)
我还建议您自己mutableListOf()
实例化一个ArrayList
。Kotlin 有合理的默认值,而且更容易阅读。无论哪种方式,它最终都会做同样的事情。
只要可能,最好使用val
over var
。
更新:Vals 不是 vars,感谢您发现 Alexey
归档时间: |
|
查看次数: |
15481 次 |
最近记录: |