Pritansh Chandra
Pritansh Chandra

Reputation: 11

How to store the Initial Attendance Data from a Recycler View(Card View) before finally publishing it on Firebase Realtime Database?

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();

    }
}

Upvotes: -1

Views: 587

Answers (2)

Niyas
Niyas

Reputation: 767

First initialise a hashMap variable. And in each click add student id and details into hashmap. Finally when teacher clicks the save button use hashmap variable for update data.

Upvotes: 0

tobzilla90
tobzilla90

Reputation: 657

As far as I understand, you have a card for every student in the class. You should create a Custom Adapter for your Recycler View it. The Adapter returns a Card view for every student. The data is stored an Array list which you pass though the Adapter. The onClick event on the Card which you can specify in the Adapter change the Attendance data of the array. At the end you push the Data to the Firebase Database when he FAB Button is clicked.

You can check this blog post to see how you create a custom list view adapter: https://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/

Upvotes: 0

Related Questions