【问题标题】:How can i pass user id from my adapter to activity如何将用户 ID 从我的适配器传递到活动
【发布时间】:2020-05-21 05:44:38
【问题描述】:

我正在尝试将持有者 user.getID 传递给我的出勤活动我如何将用户 ID 从我的适配器传递给出勤活动

这是我的出勤活动

        reference = FirebaseDatabase.getInstance().getReference("parent");

        intent = getIntent();
        final String currentSubject = intent.getStringExtra("currentSubject");
        reference.addValueEventListener(new ValueEventListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    final User user = snapshot.getValue(User.class);

                    ZoneId zonedId = ZoneId.of("America/Montreal");
                    final LocalDate today = LocalDate.now(zonedId);

                        reference = FirebaseDatabase.getInstance().getReference("attendance").child(user.getSchoolcode()).child(grade.getText().toString() + " " + section.getText().toString()).child(currentSubject).child(today.toString()).child(userID);

                        reference.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                HashMap<String, Object> hashMap = new HashMap<>();
                                hashMap.put("attendance", "yes");
                                reference.updateChildren(hashMap);
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });
                    }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }

我想把它从适配器传递给这个 这是我的适配器

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {

    private Context context;
    private List<User> users;


    public StudentAdapter(Context context, List<User> users) {
        this.context = context;
        this.users = users;
    }
    DatabaseReference reference;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.student_item, parent, false);
        return new StudentAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final User user = users.get(position);

        holder.studentName.setText(user.getStudentname() + " " + user.getLastname());

    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView studentName;
        public Switch aSwitch;

        public ViewHolder(View itemView) {
            super(itemView);

            studentName = itemView.findViewById(R.id.studentName);
            aSwitch = itemView.findViewById(R.id.attendanceSwitch);
        }
    }

这是我拥有 recyclerview 的适配器,我正在尝试获取在出勤活动 recyclerview 上查看的用户的 ID

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    您可以使用的一种方式是callback

    在您的adapter 中定义这样的接口:

    public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
    
     private SendCallBack mCallback;
    
        interface SendCallBack {
            void send(int id);
        }
    
    //Initial callback in constructor
       public StudentAdapter(Context context, List<User> users,SendCallBack callback) {
            this.context = context;
            this.users = users;
            mCallback = callback;
        }
    
    //Now in where that you want to pass data use this
    
        mCallback.send(user.getID);
    
    }
    

    现在在您的activity 中像这样实现callback

    public class YourActivty implements  YourAdapter.SendCallback {
    
        @Override
        public void send(int id) {
    
    
        }
    
    //For pass callBack for your adapter constructor use this
    
    }
    

    对于其他方式,您可以使用this 链接。

    【讨论】:

    • 我不想打开新活动
    • 然后你可以使用回调,你可以在我的答案底部使用这个链接。@Tripping
    • @Tripping 有用吗?
    • mCallback = callback;上显示可兼容类型
    • ...只需将Callback mCallback; 更改为SendCallBack mCallback; 在您的Adapter 中,并记住使用您已声明的类的对象。
    猜你喜欢
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多