簡體   English   中英

將列表大小從活動轉移到適配器 class 並使用該數量更新 TextView

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

我的FollowersActivity中有一個方法 class getFriendsAttendingEvent(); 它返回符合兩個特定條件的用戶數。 現在,當我單擊 Adapter class PostAdapter中的TextView時,這些用戶會填充一個列表。

我需要獲取列表中的用戶數(數量),我想將它放在我的PostAdapter class 中的TextView中。我想做一些類似holder.textView.setText(*list.size())的事情,但是我怎樣才能從我的FollowersActivity得到這個數字到我的PostAdapter class?

我從 Adapter class 知道我使用Intent在兩個類之間傳輸數據,但是反過來到 go,我是否必須創建一個接口或其他東西? 也許共享首選項? 我不知道...

我將如何發送這一行, 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);
        });

所以到目前為止我所了解的是,

您有一個PostAdapter (顯示與大多數社交媒體應用程序中的帖子相關的數據),並且此適配器有一個TextView表示關注者的數量。

所以,導航結構可能是這樣的:

PostAdpater ---(點擊TextView )---> FollowersActivity

通過單擊代表關注者數量的TextView ,我們將轉移到FollowersActivity ,您可能會在其中顯示關注者的詳細信息。 但問題是,您在FollowersActivity上獲取數據,這是在PostAdapter上導航之前需要的。

我能想出的唯一簡單解決方案是將查詢 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)

請告訴我,如果沒有正確理解問題,我可能會想出更好的解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM