简体   繁体   English

将列表大小从活动转移到适配器 class 并使用该数量更新 TextView

[英]Transfer list size from Activity to Adapter class and update TextView with that quantity

I have a method in my FollowersActivity class getFriendsAttendingEvent();我的FollowersActivity中有一个方法 class getFriendsAttendingEvent(); which returns the number of users that adhere to two specific criteria.它返回符合两个特定条件的用户数。 Now, those users populate a list when I click on a TextView which I have in my Adapter class PostAdapter .现在,当我单击 Adapter class PostAdapter中的TextView时,这些用户会填充一个列表。

I need to get that number (quantity) of users that are in the list and I want to put it in the TextView in my PostAdapter class. I want to do something like holder.textView.setText(*list.size()) , but how can I get that number from my FollowersActivity over to my PostAdapter class?我需要获取列表中的用户数(数量),我想将它放在我的PostAdapter class 中的TextView中。我想做一些类似holder.textView.setText(*list.size())的事情,但是我怎样才能从我的FollowersActivity得到这个数字到我的PostAdapter class?

I know from the Adapter class I transfer data between the two classes by using Intent , but to go the other way around, would I have to create an interface or something?我从 Adapter class 知道我使用Intent在两个类之间传输数据,但是反过来到 go,我是否必须创建一个接口或其他东西? SharedPreferences perhaps?也许共享首选项? I don't know...我不知道...

How would I send this line, String number = String.valueOf(mIdList.size());我将如何发送这一行, String number = String.valueOf(mIdList.size()); over to my PostAdapter ?转到我的PostAdapter吗? This is the number that I need.这是我需要的号码。

FollowersActivity粉丝活动

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) {

            }
        });
    }

PostAdapter邮政适配器

@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);
        });

So what I have understood so far is that,所以到目前为止我所了解的是,

You have a PostAdapter (that displays data related to a post just like in most of the social media applications) and this Adapter has a TextView that represents the number of followers.您有一个PostAdapter (显示与大多数社交媒体应用程序中的帖子相关的数据),并且此适配器有一个TextView表示关注者的数量。

So, the navigation structure might be like this:所以,导航结构可能是这样的:

PostAdpater --- (Click on the TextView ) ---> FollowersActivity PostAdpater ---(点击TextView )---> FollowersActivity

By Clicking the TextView representing the number of followers we shift to the FollowersActivity , and there you might be displaying the details of Followers.通过单击代表关注者数量的TextView ,我们将转移到FollowersActivity ,您可能会在其中显示关注者的详细信息。 But the problem is, that you are fetching the data on the FollowersActivity where as you required it before the navigation on the PostAdapter .但问题是,您在FollowersActivity上获取数据,这是在PostAdapter上导航之前需要的。

The Only Simple Solution that I am able to figure out is that you move your query function getFriendsAttendingEvent() from FollowersActivity to PostAdapter , fetch the count of followers in the adapter and then pass that count to the FollowersActivity through Intent in case you do not want to re fetch data.我能想出的唯一简单解决方案是将查询 function getFriendsAttendingEvent()FollowersActivity移动到PostAdapter ,获取适配器中的关注者计数,然后通过 Intent 将该计数传递给FollowersActivity以防万一重新获取数据。

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

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

Please tell me if didn't understood the problem correctly i might come up with a better solution请告诉我,如果没有正确理解问题,我可能会想出更好的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM