Android 如何将文本添加到kotlin中的数组中
当用户单击“添加”按钮时,我试图从EditText向数组中添加新文本。 到目前为止,我已经从字符串文件中获得了硬编码文本,但我希望在用户在edittext字段中键入文本并按下add按钮时能够添加新类别 这是我的班级:Android 如何将文本添加到kotlin中的数组中,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,当用户单击“添加”按钮时,我试图从EditText向数组中添加新文本。 到目前为止,我已经从字符串文件中获得了硬编码文本,但我希望在用户在edittext字段中键入文本并按下add按钮时能够添加新类别 这是我的班级: package com.example.myapplication import android.content.Context import androidx.appcompat.app.AppCompatActivity import android.os.Bundle im
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)
}
}
package com.example.myapplication
导入android.content.Context
导入androidx.appcompat.app.appcompat活动
导入android.os.Bundle
导入android.view.inputmethod.InputMethodManager
导入android.widget.Button
导入android.widget.EditText
导入android.widget.Toast
导入androidx.recyclerview.widget.LinearLayoutManager
导入androidx.recyclerview.widget.recyclerview
导入com.example.myapplication.Model.MyModel
导入com.example.myapplication.adapters.Category
类配置文件:AppCompatActivity(){
lateinit变量类别:EditText
lateinit变量添加:按钮
lateinit变量类别列表:RecyclerView
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\U配置文件)
类别=findViewById(R.id.add\u类别)
add=findViewById(R.id.add_按钮)
//设置回收器视图
val categoryModelArrayList=populateList()
val recyclerView=findviewbyd(R.id.categoryLists)
val layoutManager=LinearLayoutManager(此)
recyclerView.layoutManager=layoutManager
val适配器=类别(categoryModelArrayList)
recyclerView.adapter=适配器
add.setOnClickListener{
val newCategory=category.text.toString().trim()
val isValid=validateCategory(新类别)
如果(有效){
解雇键盘()
Toast.makeText(此“$newCategory已添加到以下列表中”,Toast.LENGTH\u LONG.show()
//当用户单击add按钮时,需要将$newCategory添加到以下列表中
}
}
}
private fun populateList():ArrayList{
val list=ArrayList()
val categoryList=arrayOf(R.string.app_name,R.string.bottom_sheet_behavior,R.string.saved_articles)
val size=类别列表大小
对于(类别列表索引中的i){
val categoryModel=MyModel()
setCategories(getString(categoriList[i]))
list.add(categoryModel)
}
返回列表
}
private fun validateCategory(新类别:字符串):布尔值{
if(newCategory.isEmpty()){
category.setError(“类别是必需的”)
返回错误
}
返回真值
}
私人娱乐键盘(){
val view=this.currentFocus
如果(视图!=null){
val imm=getSystemService(Context.INPUT\u METHOD\u SERVICE)作为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)
)