android - 如何将文本添加到 kotlin 的数组中
问题描述
当用户单击添加按钮时,我试图将新文本从 EditText 添加到数组中。到目前为止,我已经从我的字符串文件中获得了硬编码文本,但是当用户在 edittext 字段中键入并按下添加按钮时,我希望能够添加新类别
这是我的课:
package com.example.myapplication
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.Model.MyModel
import com.example.myapplication.adapters.Category
class Profile : AppCompatActivity() {
lateinit var category: EditText
lateinit var add: Button
lateinit var categoryList: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
category = findViewById<EditText>(R.id.add_category)
add = findViewById<Button>(R.id.add_button)
//Set Recycler view
val categoryModelArrayList = populateList()
val recyclerView = findViewById<RecyclerView>(R.id.categoryLists)
val layoutManager = LinearLayoutManager(this)
recyclerView.layoutManager = layoutManager
val adapter = Category(categoryModelArrayList)
recyclerView.adapter = adapter
add.setOnClickListener {
val newCategory = category.text.toString().trim()
val isValid = validateCategory(newCategory)
if (isValid) {
dismissKeyBoard()
Toast.makeText(this, "$newCategory has been added to your list of following", Toast.LENGTH_LONG).show()
//Need to add $newCategory to the following list when the user clicks on the add button
}
}
}
private fun populateList() : ArrayList<MyModel> {
val list = ArrayList<MyModel>()
val categoryList = arrayOf(R.string.app_name, R.string.bottom_sheet_behavior, R.string.saved_articles)
val size = categoryList.size
for (i in categoryList.indices) {
val categoryModel = MyModel()
categoryModel.setCategories(getString(categoryList[i]))
list.add(categoryModel)
}
return list
}
private fun validateCategory(newCategory:String): Boolean {
if(newCategory.isEmpty()) {
category.setError("Category is required")
return false
}
return true
}
private fun dismissKeyBoard() {
val view = this.currentFocus
if (view != null) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
}
所以因为我只是使用我的应用程序的字符串文件中存在的字符串但是我希望能够在用户按下添加按钮时添加新字符串但是当我尝试添加新字符串时它说它需要一个 Int 而不是一个字符串,我不知道为什么
我将如何向这个数组添加新字符串?
解决方案
你在这里创建了一个 Int 数组
val categoryList = arrayOf(R.string.app_name, R.string.bottom_sheet_behavior, R.string.saved_articles)
R.string.anything
是资源的整数 id,而不是资源本身。
如果你想要一个包含实际值的字符串数组,你需要这样的东西:
val categoryList = arrayOf(
getString(R.string.app_name),
getString(R.string.bottom_sheet_behavior),
getString(R.string.saved_articles)
)
推荐阅读
- angularjs - 无法从 AngularJS Scope 中获取价值
- java - 使用媒体播放器播放 .M3U 链接
- c# - 如何使用日期范围获取目录中的文件?
- nlog - Xml 配置中的 NLog default-target-parameters 不适用于以编程方式添加的文件目标
- python-3.x - Boto3 - get_change()、change_resource_record_set() INSYNC 需要 60 秒才能返回
- javascript - 使用函数访问类方法
- python - 从模型创建表单中的相关模型字段中删除“删除选定模型”按钮(Django 管理员)
- c# - 如何将公共(全局)变量分配给 SwitchCell 的 OnProperty(isOn 绑定)以检查是否启用了 SwichCell(打开)
- java - 在 Debug Shell 中执行代码时出现 Eclipse photon 错误
- permissions - 尽管设置了额外的权限,Odoo 11 用户仍无法创建新联系人