验证在firebase回收器适配器中不起作用。

问题描述 投票:-2回答:1

我正在应用验证,从数据库中获取列表中的所有标记,然后在onBindviewHolder中应用Foreach循环,将所有标记与当前用户标记进行匹配,如果标记匹配,则项目视图不应显示在recyclerview中。

for (DataSnapshot parent : dataSnapshot.getChildren()) {
                    String token;
                    token = parent.getKey();
                    tokenList.add(token);
                }

在这里显示列表。

 for (String tokens : tokenList) {

                    if (bloodGroupMatched.contains(bloodGroup) && !tokens.equals(userId)) {

                        holder.name.setText(profiles.getName());
                        holder.type.setText(userType);
                        holder.distance.setText(dist + " km");
                        holder.itemView.setVisibility(View.VISIBLE);
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        params.setMargins(10, 5, 5, 10);
                        holder.itemView.setLayoutParams(params);
                    } else {

                        holder.itemView.setVisibility(View.GONE);
                        holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
                    }

                }

在显示列表的时候,如果我把token烤焦,循环就不会结束,它一直从列表结束时开始显示token,而且不在recyclerview中显示任何东西.我想显示除了不符合标准的用户之外的用户列表.如果我删除这个检查"!tokens.equals(userId) "并删除循环,它就会开始正常工作,但我也必须应用这个验证。enter image description here

我正在获取这些节点,并且哪个节点与当前登录用户节点相匹配,我就不想显示他的详细信息,这就是我使用这个逻辑的整个函数。

adapter = new FirebaseRecyclerAdapter<Profiles, DonarViewHolder>(model) {
            @Override
            protected void onBindViewHolder(@NonNull DonarViewHolder holder, int position, @NonNull Profiles profiles) {

                ArrayList<String> bloodGroupMatched = new ArrayList<>();
                bloodGroupMatched.addAll(profiles.getMatched_bloodGroups());

                for (String tokens : tokenList) {

                    if (bloodGroupMatched.contains(bloodGroup) && !tokens.equals(userId)) {

                        holder.name.setText(profiles.getName());
                        holder.itemView.setVisibility(View.VISIBLE);
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        params.setMargins(10, 5, 5, 10);
                        holder.itemView.setLayoutParams(params);
                    } else {

                        holder.itemView.setVisibility(View.GONE);
                        holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
                    }
                }
            }

            @NonNull
            @Override
            public DonarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_donar_view_holder, parent, false);
                return new DonarViewHolder(view);
            }
        };
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter.startListening();
        progressDialog.dismiss();
        recyclerView.setAdapter(adapter);
android firebase firebase-realtime-database recycler-adapter
1个回答
0
投票

我通过修改我的逻辑得到了解决方法 onBindViewHolder我删除了foreach循环,并开始将用户ID与tokenlist位置进行匹配。

   ArrayList<String> bloodGroupMatched = new ArrayList<>();
        bloodGroupMatched.addAll(profiles.getMatched_bloodGroups());


        if (bloodGroupMatched.contains(bloodGroup) && !userId.equals(tokenList.get(position))){

            holder.name.setText(profiles.getName());
            holder.type.setText(userType);
            holder.distance.setText(dist + " km");
            holder.itemView.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(10, 5, 5, 10);
            holder.itemView.setLayoutParams(params);
        } else {

            holder.itemView.setVisibility(View.GONE);
            holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
        }
© www.soinside.com 2019 - 2024. All rights reserved.