我是Kotlin编码的初学者,我正在尝试制作一个带有编辑文本的搜索栏,该文本可从火力基地找到用户帐户。这是我的密码
Route::resource('users', 'AdminUserController')->parameters([
'users' => 'admin_user'
]);
打包com.example.videoapp.Model
Route::resource('days', 'OperatingDayController')
->names([
'index' => 'admin.days.index',
'store' => 'admin.days.store',
'create' => 'admin.days.create',
'show' => 'admin.days.show',
'update' => 'admin.days.update',
'destroy' => 'admin.days.destroy',
'edit' => 'admin.days.edit',
])
->parameters(['days' => 'operatingDay']);
但是每当我在搜索中输入任何单词时,它都会返回错误
package com.example.videoapp
import android.annotation.SuppressLint
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.videoapp.Adapter.UserAdapter
import com.example.videoapp.Model.Users
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.*
import kotlinx.android.synthetic.main.fragment_explore.*
import kotlinx.android.synthetic.main.fragment_explore.view.*
import kotlinx.android.synthetic.main.fragment_explore.view.recyclerView
import java.util.*
import kotlin.collections.ArrayList
/**
* A simple [Fragment] subclass.
*/
class ExploreFragment : Fragment() {
private var recyclerView: RecyclerView?=null
private var userAdapter:UserAdapter?=null
private var mUser: List<Users>?=null
private var searchEditText: EditText?=null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view= inflater.inflate(R.layout.fragment_explore, container, false)
recyclerView = view.findViewById(R.id.recyclerView)
recyclerView!!.setHasFixedSize(true)
recyclerView!!.layoutManager = LinearLayoutManager(context)
searchEditText = view.findViewById(R.id.search_edit_text)
mUser = ArrayList()
retrieveAllUserd()
searchEditText!!.addTextChangedListener(object :TextWatcher{
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(cs: CharSequence?, start: Int, before: Int, count: Int) {
searchForUser(cs.toString().toLowerCase())
}
})
return view
}
private fun retrieveAllUserd() {
var firebaseUserID = FirebaseAuth.getInstance().currentUser!!.uid
val refUser = FirebaseDatabase.getInstance().reference.child("Users").child(firebaseUserID)
refUser.addValueEventListener(object :ValueEventListener{
override fun onDataChange(p0: DataSnapshot) {
(mUser as ArrayList<Users>).clear()
if (searchEditText!!.text.toString() == ""){
for (snapshot in p0.children){
val user = snapshot.getValue(Users::class.java)
if (!(user!!.getid()).equals(firebaseUserID)){
(mUser as ArrayList<Users>).add(user)
}
}
}
userAdapter = UserAdapter(
mContext = context!!,
mUser = mUser!!,
isFragment = false
)
recyclerView!!.adapter = userAdapter
}
override fun onCancelled(p0: DatabaseError) {
}
})
}
private fun searchForUser(str: String){
var firebaseUserID = FirebaseAuth.getInstance().currentUser!!.uid
val queryUserd = FirebaseDatabase.getInstance().reference.child("Users").orderByChild("name").startAt(str)
.endAt(str+"\uf8ff")
queryUserd.addValueEventListener(object : ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
(mUser as ArrayList<Users>).clear()
for (snapshot in p0.children){
val user = snapshot.getValue(Users::class.java)
if (!(user!!.getid()).equals(firebaseUserID)) {
(mUser as ArrayList<Users>).add(user)
}
}
userAdapter = UserAdapter(context!!,mUser!!,false)
recyclerView!!.adapter = userAdapter
}
})
}
}
at java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:0)
这就是声明User
类所需要的:
class Users(
var username:String = " "
var Fullname:String = " "
var image:String = " "
var id:String = " "
)
Getters和Setters由Kotlin编译器自动生成。
答案 1 :(得分:0)
尝试摆脱getter和setter并使用data
类:
data class User(
var username: String = " ",
var fullName: String = " ",
var image: String = " ",
var id: String = " "
)
主要构造函数的参数,例如用户名,请成为properties,因此您无需为其编写其他的getter和setter函数。
答案 2 :(得分:0)
Kotlin编译器为类中定义的只读变量(用于读写变量的getter和setter)生成getter。因为您有一个名为fullname
的变量以及自定义getfullname
和setfullname
,所以您将有两个fullname
变量的获取器和两个设置器。
您将在Java代码中看到所有get
方法:
看起来snapshot.getValue(Users::class.java)
不知道要使用哪个getters
和setters
。
简单解决方案应删除自定义方法:
class Users {
var username:String = " "
var Fullname:String = " "
var image:String = " "
var id:String = " "
constructor()
constructor(username: String, fullname: String, image: String, id: String) {
this.username = username
this.Fullname = fullname
this.image = image
this.id = id
}
}
否则,请使用private
访问修饰符更新变量声明:
class Users {
private var username:String = " "
private var Fullname:String = " "
private var image:String = " "
private var id:String = " "
constructor()
constructor(username: String, fullname: String, image: String, id: String) {
this.username = username
this.Fullname = fullname
this.image = image
this.id = id
}
// Below are your custom getters and setters
}