帮我写代码用Android Studio检索框:来访日期、被访人姓名 列表内容:从数据库查出本人的预约记录,以列表形式展示预约记录,列表中展示提交时间、姓名、来访开始时间、来访结束时间、被访人部门、被访人姓名
时间: 2024-10-27 16:02:55 浏览: 14
在Android Studio中,为了从数据库获取预约记录并以列表形式展示,你可以使用RecyclerView以及Room Persistence Library(如果使用SQLite数据库)来管理数据。这里是一个简化的示例代码,假设我们有一个`Appointment`实体类,包含了你需要的信息:
```java
// Appointment.java
public class Appointment {
private Long id;
private String submitTime;
private String visitorName;
private LocalDateTime visitStartTime;
private LocalDateTime visitEndTime;
private String visitorDepartment;
private String visitorContact;
// getters and setters
}
// DatabaseHelper.kt (如果使用Room)
@Dao
interface DatabaseHelper {
@Query("SELECT * FROM appointments WHERE owner_id = :userId")
LiveData<List<Appointment>> getAppointmentsForUser(long userId);
}
// AppointmentAdapter.kt
class AppointmentAdapter(private val appointmentList: List<Appointment>) :
RecyclerView.Adapter<AppointmentAdapter.AppointmentViewHolder>() {
// ViewHolder setup
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppointmentViewHolder {
return AppointmentViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_appointment, parent, false))
}
// Inflate the layout for each item and bind data
override fun onBindViewHolder(holder: AppointmentViewHolder, position: Int) {
val appointment = appointmentList[position]
holder.submitTimeTextView.text = appointment.submitTime
holder.visitorNameTextView.text = appointment.visitorName
holder.visitStartTimeTextView.text = appointment.visitStartTime.format(DateTimeFormat.isoDateTime())
holder.visitEndTimeTextView.text = appointment.visitEndTime.format(DateTimeFormat.isoDateTime())
holder.visitorDepartmentTextView.text = appointment.visitorDepartment
holder.visitorContactTextView.text = appointment.visitorContact
}
// Other methods like getItemCount()
}
// YourActivity.kt
val userId = /* get the current user's ID from somewhere */;
val databaseHelper = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java, "app_database"
).build().databaseHelper
databaseHelper.getAppointmentsForUser(userId)
.observe(this, Observer { appointments ->
recyclerView.adapter = AppointmentAdapter(appointments)
})
```
在这个例子中,首先通过`DatabaseHelper`查询用户的预约记录,然后在`AppointmentAdapter`中将数据绑定到RecyclerView的每个条目上。
阅读全文