简体   繁体   中英

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

I have a method in my 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 .

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?

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? SharedPreferences perhaps? I don't know...

How would I send this line, String number = String.valueOf(mIdList.size()); over to my 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.

So, the navigation structure might be like this:

PostAdpater --- (Click on the 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. But the problem is, that you are fetching the data on the FollowersActivity where as you required it before the navigation on the 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.

      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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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