import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.studentregister.db.Student
class StudentRecyclerViewAdapter(
private val clickListener:(Student)->Unit
) : RecyclerView.Adapter<StudentViewHolder>(){
private val studentList=ArrayList<Student>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StudentViewHolder {
val layoutInflater=LayoutInflater.from(parent.context)
val listItem=layoutInflater.inflate(R.layout.list_item,parent,false)
return StudentViewHolder(listItem)
}
override fun onBindViewHolder(holder: StudentViewHolder, position: Int) {
holder.bind(studentList[position],clickListener)
}
override fun getItemCount(): Int {
return studentList.size
}
fun setList(students:List<Student>){
studentList.clear()
studentList.addAll(students)
}
}
class StudentViewHolder(private val view:View):RecyclerView.ViewHolder(view){
fun bind(student: Student,clickListener:(Student)->Unit){
val nametextview=view.findViewById<TextView>(R.id.tvname)
val emailtextview=view.findViewById<TextView>(R.id.tvemail)
nametextview.text = student.name
emailtextview.text = student.email
view.setOnClickListener {
clickListener(student)
}
}
}
我正在 kotlin 中构建一个学生姓名电子邮件注册应用程序,该信息保存在数据库中并显示在回收站视图中我想在回收站视图顶部显示最新添加的信息,所有最新添加的记录将显示在顶部而不是回收站视图结束 我想反转回收站视图 这是我的主要活动代码
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.studentregister.db.Student
import com.example.studentregister.db.StudentDatabase
class MainActivity : AppCompatActivity() {
private lateinit var nameedittext:EditText
private lateinit var emailedittext:EditText
private lateinit var savebutton:Button
private lateinit var clearbutton:Button
private lateinit var viewModel: StudentViewModel
private lateinit var studentRecyclerView: RecyclerView
private lateinit var adapter: StudentRecyclerViewAdapter
private var islistitemClicked=false
private lateinit var selectedStudent: Student
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nameedittext=findViewById(R.id.etname)
emailedittext=findViewById(R.id.etemail)
savebutton=findViewById(R.id.btnsave)
clearbutton=findViewById(R.id.btnclear)
studentRecyclerView=findViewById(R.id.rvstudent)
val dao=StudentDatabase.getInstance(application).studentDao()
val factory=StudentViewModelFactory(dao)
viewModel=ViewModelProvider(this,factory).get(StudentViewModel::class.java)
savebutton.setOnClickListener {
if (islistitemClicked) {
updatestudentdata()
clearInput()
} else {
saveStudentData()
clearInput()
}
}
clearbutton.setOnClickListener {
if(islistitemClicked){
deletestudentdata()
clearInput()
}else{
clearInput()
}
}
initRecyclerView()
}
private fun saveStudentData(){
viewModel.insertStudent(
Student(
0,
nameedittext.text.toString(),
emailedittext.text.toString()
)
)
}
private fun updatestudentdata(){
viewModel.updateStudent(
Student(
selectedStudent.id,
nameedittext.text.toString(),
emailedittext.text.toString()
)
)
savebutton.text="Save"
clearbutton.text="Clear"
islistitemClicked = false
}
private fun deletestudentdata(){
viewModel.deleteStudent(
Student(
selectedStudent.id,
nameedittext.text.toString(),
emailedittext.text.toString()
)
)
savebutton.text="Save"
clearbutton.text="Clear"
islistitemClicked = false
}
private fun clearInput(){
nameedittext.setText("")
emailedittext.setText("")
}
private fun initRecyclerView(){
studentRecyclerView.layoutManager=LinearLayoutManager(this)
adapter = StudentRecyclerViewAdapter{
selectedItem:Student -> listitemclicked(selectedItem)
}
studentRecyclerView.adapter=adapter
displayStudentList()
}
private fun displayStudentList(){
viewModel.Students.observe(this,{
adapter.setList(it)
adapter.notifyDataSetChanged()
})
}
private fun listitemclicked(student: Student){
//Toast.makeText(this,"student name is ${student.name}",Toast.LENGTH_LONG).show()
selectedStudent = student
savebutton.text="Update"
clearbutton.text="Delete"
islistitemClicked = true
nameedittext.setText(selectedStudent.name)
emailedittext.setText(selectedStudent.email)
}
}
我用 Java 解决了这个问题。 我通过反转整个位置来做到这一点。 我的代码是:
hashMap = arrayList.get((arrayList.size() - 1) - position);
上面的代码从顶部开始显示最后一项。