我正在为学生和讲师创建约会管理系统。我有一个数组listview,它从firebase检索数据。我包括两个按钮,可将约会状态更改为接受或拒绝。按下按钮时,我找不到在列表视图上更新Firebase数据的代码。
我实际上是android开发的新手,所以如果我问易于回答的问题,请原谅我。我已尽一切努力找到解决问题的方法,但似乎找不到。这是一个约会管理系统,学生可以在其中与讲师进行约会。对于讲师视图,我有一个列表视图,其中显示了所有学生预约的请求。每行将有2组按钮,用于接受或拒绝约会。当按下任一按钮时,它将更新约会的状态,例如;按批准按钮状态变为批准。从Firebase实时数据库中检索数据。我的问题是,当同时按下两个按钮时,我找不到合适的代码来更新约会的状态。我真的需要帮助。提前致谢。
这是我的活动。
tag
}
这是我的适配器视图
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.lect_fragment_appointment, container, false);
myrecyclerview= (RecyclerView) v.findViewById(R.id.appointment_list_recyclerview2);
myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
list = new ArrayList<ListView_Appointmnet_LectView>();
SharedPreferences preferences = this.getActivity().getSharedPreferences("user_details", Context.MODE_PRIVATE);
databaseReference = FirebaseDatabase.getInstance().getReference().child("Appointment");
Query query1 = databaseReference.orderByChild("lect_name").equalTo(preferences.getString("Lect_Name",null));
query1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
{
ListView_Appointmnet_LectView L = dataSnapshot1.getValue(ListView_Appointmnet_LectView.class);
list.add(L);
}
adapter = new CustomAdapter_ListView_Lecturer(getContext(),list);
myrecyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
Setter和getter类:
public class CustomAdapter_ListView_Lecturer extends RecyclerView.Adapter<CustomAdapter_ListView_Lecturer.MyViewHolder> {
Context context;
ArrayList <ListView_Appointmnet_LectView> appointment_lectview ;
public CustomAdapter_ListView_Lecturer(Context c, ArrayList<ListView_Appointmnet_LectView> a)
{
context = c;
appointment_lectview = a;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_appointment_list_lecturer, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) {
myViewHolder.name.setText(appointment_lectview.get(position).getStudent_ID());
myViewHolder.subject.setText(appointment_lectview.get(position).getSubject());
myViewHolder.date.setText(appointment_lectview.get(position).getStart_date());
myViewHolder.time.setText(appointment_lectview.get(position).getStart_time());
myViewHolder.onClick(position);
}
@Override
public int getItemCount() {
return appointment_lectview.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView name, date, subject, time, status;
ImageButton approve_btn, reject_btn;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.appointment_studentID);
date = (TextView) itemView.findViewById(R.id.appointment_date);
subject = (TextView) itemView.findViewById(R.id.appointment_subject);
time = (TextView) itemView.findViewById(R.id.appointment_time);
status = (TextView) itemView.findViewById(R.id.appointment_status);
approve_btn = (ImageButton) itemView.findViewById(R.id.approve_btn);
reject_btn = (ImageButton) itemView.findViewById(R.id.reject_btn);
}
public void onClick(final int position) {
approve_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Approve " + position + "is clicked", Toast.LENGTH_SHORT).show();
}
});
reject_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Reject " + position + "is clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
}
答案 0 :(得分:0)
当用户在“回收者”视图中单击某个项目时,您会被告知他们单击的项目的位置/索引。因此,您将需要以某种方式将该位置映射回Firebase实时数据库中该项目的键。
一种简单的方法是保留快照的值和密钥。现在,您的代码将忽略键,仅获取值。因此,通过添加带有键的额外列表,您可以在单击某项时查找键。
您需要修改的主要代码是onDataChange
:
query1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
{
ListView_Appointmnet_LectView L = dataSnapshot1.getValue(ListView_Appointmnet_LectView.class);
list.add(L);
String key = dataSnapshot1.getKey();
keyList.add(key);
}
要使其正常工作,您需要:
keyList
添加一个onCreateView
变量。CustomAdapter_ListView_Lecturer
一个成员字段来保存密钥列表。keyList
传递到CustomAdapter_ListView_Lecturer
构造函数中,并设置成员字段。按其位置查找被单击项的键:
public void onClick(final int position) {
approve_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Approve " + position + "is clicked", Toast.LENGTH_SHORT).show();
String key = adapter.mKeyList.get(position);
根据项目的密钥更新项目:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Appointment");
ref.child(key).child("status").setValue("NEW STATUS");