将列表大小从 Activity 转移到适配器 class 并使用该数量更新 TextView
Transfer list size from Activity to Adapter class and update TextView with that quantity
我在 FollowersActivity
class getFriendsAttendingEvent();
中有一个方法 returns 遵守两个特定标准的用户数量。现在,当我单击适配器 class PostAdapter
中的 TextView
时,这些用户会填充一个列表。
我需要获取列表中的用户数(数量),我想将其放入我的 PostAdapter
class 中的 TextView
中。我想做一些类似 holder.textView.setText(*list.size())
的事情,但是我怎样才能从我的 FollowersActivity
得到那个数字到我的 PostAdapter
class?
我从适配器得知 class 我使用 Intent
在两个 class 之间传输数据,但是反过来,我是否必须创建一个接口或者其他的东西?也许共享首选项?我不知道...
我如何将这条线 String number = String.valueOf(mIdList.size());
发送到我的 PostAdapter
?这是我需要的号码。
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);
});
到目前为止我的理解是,
你有一个 PostAdapter
(显示与 post 相关的数据,就像在大多数社交媒体应用程序中一样)并且这个适配器有一个 TextView
代表关注者。
所以,导航结构可能是这样的:
PostAdpater
---(点击TextView
)---> FollowersActivity
通过单击代表关注者数量的 TextView
,我们将转移到 FollowersActivity
,您可能会在此处显示关注者的详细信息。但问题是,在 PostAdapter
.
上导航之前,您正在根据需要在 FollowersActivity
上获取数据
我能想出的唯一简单解决方案是将查询函数 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)
请告诉我如果没有正确理解问题,我可能会想出更好的解决方案
我在 FollowersActivity
class getFriendsAttendingEvent();
中有一个方法 returns 遵守两个特定标准的用户数量。现在,当我单击适配器 class PostAdapter
中的 TextView
时,这些用户会填充一个列表。
我需要获取列表中的用户数(数量),我想将其放入我的 PostAdapter
class 中的 TextView
中。我想做一些类似 holder.textView.setText(*list.size())
的事情,但是我怎样才能从我的 FollowersActivity
得到那个数字到我的 PostAdapter
class?
我从适配器得知 class 我使用 Intent
在两个 class 之间传输数据,但是反过来,我是否必须创建一个接口或者其他的东西?也许共享首选项?我不知道...
我如何将这条线 String number = String.valueOf(mIdList.size());
发送到我的 PostAdapter
?这是我需要的号码。
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);
});
到目前为止我的理解是,
你有一个 PostAdapter
(显示与 post 相关的数据,就像在大多数社交媒体应用程序中一样)并且这个适配器有一个 TextView
代表关注者。
所以,导航结构可能是这样的:
PostAdpater
---(点击TextView
)---> FollowersActivity
通过单击代表关注者数量的 TextView
,我们将转移到 FollowersActivity
,您可能会在此处显示关注者的详细信息。但问题是,在 PostAdapter
.
FollowersActivity
上获取数据
我能想出的唯一简单解决方案是将查询函数 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)
请告诉我如果没有正确理解问题,我可能会想出更好的解决方案