-1

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

    }
}

  • Instead of describing your code, edit your question to include the minimal, complete/standalone code that is needed to reproduce the problem. See [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) for information on why that is useful, and how to go a about it. – Frank van Puffelen Aug 17 '19 at 16:15
  • I am asking for the logic it's not a problem to reproduce it. – Pritansh Chandra Aug 17 '19 at 16:17

2 Answers2

0

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/

tobzilla90
  • 607
  • 5
  • 9
  • I have already created Adapter and all the stuff you mentioned. i only need to know the logic after the onClick. When i click the card of say student 2, 3 &5, i need them to be marked Present initially and the rest will be marked Absent. i only need to know where and how to store this data on the device initially before pushing it to Firebase – Pritansh Chandra Aug 17 '19 at 16:31
  • you can extend the Student List class with a boolean for the attendance. Set it to false by default and change it in the onClick of the card. – tobzilla90 Aug 18 '19 at 01:00
0

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.

Niyas
  • 717
  • 11
  • 18