我是 Android 开发的新手,一开始很害羞地问这个问题,但是没有你们的帮助我做不到,所以就在这里。
我正在尝试为我的大学制作一个出勤应用程序。现在我已经完成了基本元素的工作,但主要问题是记录出勤并保存它。
我正在做的是我在 Firebase 实时数据库中创建了学生信息(姓名和卷号),然后我将该信息提取到我的 CardView 中的回收站视图中。我想要做的是我希望所有学生都被默认标记为缺席,当老师点击任何卡片时,初始信息以及StudAttendance =“P”和其余学生信息以及StudAttendance =“A”存储在某处,当他点击保存出勤FAB时,最终出勤被推送到AttendanceList>currentDate>内的firebase。
我只想知道在卡片的 OnClick 方法中要做什么。
这是我的代码
public class takeAttendance extends AppCompatActivity {
DatabaseReference AttendanceDatabaseReference;
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;
public ArrayList<StudentList> studentListArray;
//private String date = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_attendance);
//FloatingActionButton saveBtn = findViewById(R.id.floatingActionButton);
recyclerView = findViewById(R.id.mRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerViewAdapter = new RecyclerViewAdapter(studentListArray);
recyclerView.setAdapter(recyclerViewAdapter);
studentListArray = new ArrayList<>();
AttendanceDatabaseReference = FirebaseDatabase.getInstance().getReference("studentsInfo");
AttendanceDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
findViewById(R.id.progressBar).setVisibility(View.GONE);
for (DataSnapshot mDataSnapshot : dataSnapshot.getChildren()){
StudentList studentsList = mDataSnapshot.getValue(StudentList.class);
studentListArray.add(studentsList);
}
recyclerViewAdapter = new RecyclerViewAdapter(studentListArray);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
// Toast.makeText(takeAttendance.this, "Oops! Something Went Wrong...", Toast.LENGTH_LONG).show();
}
});
}
}
这是适配器类
package com.pkg.attendanceapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.textview.MaterialTextView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.StudentListViewHolder> {
private ArrayList<StudentList> studentList;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
public static class StudentListViewHolder extends RecyclerView.ViewHolder {
private MaterialTextView studentName;
private MaterialTextView studentRoll;
public StudentListViewHolder(@NonNull final View itemView, final OnItemClickListener listener) {
super(itemView);
studentName = itemView.findViewById(R.id.stdName);
studentRoll = itemView.findViewById(R.id.stdRoll);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listener!=null){
int position = getAdapterPosition();
int p = position+1;
listener.onItemClick(p);
}
}
});
}
}
public RecyclerViewAdapter(ArrayList<StudentList> mStudentList){
studentList = mStudentList;
}
@Override
public StudentListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new StudentListViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view, parent, false), mListener);
}
@Override
public void onBindViewHolder(@NonNull final StudentListViewHolder holder, int position) {
StudentList currentStudent = studentList.get(position);
holder.studentName.setText(currentStudent.getStudName());
holder.studentRoll.setText(currentStudent.getStudRoll());
}
@Override
public int getItemCount() {
return studentList == null ? 0 : studentList.size();
}
}