首页 > 解决方案 > 将列表大小从 Activity 传输到 Adapter 类并使用该数量更新 TextView

问题描述

我的FollowersActivity类中有一个方法,getFriendsAttendingEvent();它返回符合两个特定标准的用户数量。现在,当我单击TextViewAdapter 类中的一个时,这些用户会填充一个列表PostAdapter

我需要获取列表中的用户数量(数量),并且我想将它放在TextView我的PostAdapter班级中。我想做类似的事情,但我怎样才能从我的班级holder.textView.setText(*list.size())得到那个号码?FollowersActivityPostAdapter

我从 Adapter 类中知道我通过 using 在两个类之间传输数据Intent,但是反过来,我是否必须创建一个接口或其他东西?SharedPreferences 也许?我不知道...

我如何将这条线发送String number = String.valueOf(mIdList.size());到我的PostAdapter?这是我需要的号码。

追随者活动

private void getFriendsAttendingEvent() {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Attending Event").child(mPostId);
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mIdList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    mIdList.add(snapshot.getKey());
                }

                DatabaseReference reference1 = FirebaseDatabase.getInstance().getReference("Following").child(mFirebaseUser.getUid());
                reference1.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        mIdList1.clear();
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                            for (String id : mIdList) {
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                    if (Objects.equals(snapshot.getKey(), id)) {
                                        mIdList1.add(snapshot.getKey());
                                    }
                                }
                            }
                        }
                        if (mIdList1.size() > 0) {
                        String number = String.valueOf(mIdList.size());
                        Log.d("NUMBEROFUSERS", number);
                        showUsers();
                        }
                    }

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

                    }
                });
            }

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

            }
        });
    }

后适配器

@Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {

        mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        final Post post = mPost.get(position);

        holder.friendsAttendingEvent.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, FollowersActivity.class);
            intent.putExtra("id", post.getPostid());
            intent.putExtra("postid", post.getPostid());
            intent.putExtra("publisherid", post.getPublisher());
            intent.putExtra("title", "Friends Attending");
            mContext.startActivity(intent);
        });

标签: javaandroidadapter

解决方案


所以到目前为止我的理解是,

你有一个PostAdapter(就像在大多数社交媒体应用程序中一样显示与帖子相关的数据),这个适配器有一个TextView代表关注者数量的。

所以,导航结构可能是这样的:

PostAdpater---(点击TextView)--->FollowersActivity

通过单击TextView代表关注者数量的图标,我们会转到FollowersActivity,您可能会在此处显示关注者的详细信息。但问题是,您FollowersActivityPostAdapter.

我能想出的唯一简单的解决方案是,您将查询函数getFriendsAttendingEvent()FollowersActivity移至PostAdapter,获取适配器中的关注者计数,然后将该计数传递给FollowersActivity通过 Intent 以防您不想重新获取数据。

      PostAdapter         ------->         FollowersActivity
 (count required here)                      (fetching here)

      PostAdapter         ------->         FollowersActivity
  (fetch count here)         (pass data here through Intent of refetch it)

请告诉我,如果没有正确理解问题,我可能会想出更好的解决方案


推荐阅读