-1

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

    }
}

4

2 回答 2

0

据我了解,班上每个学生都有一张卡片。您应该为您的 Recycler View 创建一个自定义适配器。适配器为每个学生返回一个卡片视图。数据存储在您通过适配器传递的数组列表中。您可以在适配器中指定的卡片上的 onClick 事件更改数组的出勤数据。最后,当单击 FAB 按钮时,您将数据推送到 Firebase 数据库。

您可以查看此博客文章以了解如何创建自定义列表视图适配器: https ://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/

于 2019-08-17T16:27:49.317 回答
0

首先初始化一个 hashMap 变量。并在每次点击时将学生 ID 和详细信息添加到 hashmap 中。最后,当教师单击保存按钮时,使用 hashmap 变量更新数据。

于 2019-08-17T17:30:11.123 回答