I am new to Android Development and was very shy to ask this question at first but i couldn't do it without you guys' help so here it is.
I am trying to make an AttendanceApp for my College. Now i have made the basic elements work but the main problem is with logging the attendance and saving it.
What i am doing is that i have created the Students info ( Name and Roll No ) in Firebase Realtime Database and then i am fetching that info into my CardView inside a recycler view. What i want to do is that i want that all the students are marked absent by default and when the teacher clicks on the any of the Cards, the initial information along with the StudAttendance = "P" and the rest of the studentsinfo along with StudAttendance = "A" is stored somewhere and when he clicks on the Save Attendance FAB, the final Attendance is pushed to firebase inside AttendanceList>currentDate>.
I just want to know what to do inside of the OnClick method of the card.
Here's my Code
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();
}
});
}
}
Here's the Adapter class
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();
}
}