java - 将列表大小从 Activity 传输到 Adapter 类并使用该数量更新 TextView
问题描述
我的FollowersActivity
类中有一个方法,getFriendsAttendingEvent();
它返回符合两个特定标准的用户数量。现在,当我单击TextView
Adapter 类中的一个时,这些用户会填充一个列表PostAdapter
。
我需要获取列表中的用户数量(数量),并且我想将它放在TextView
我的PostAdapter
班级中。我想做类似的事情,但我怎样才能从我的班级holder.textView.setText(*list.size())
得到那个号码?FollowersActivity
PostAdapter
我从 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);
});
解决方案
所以到目前为止我的理解是,
你有一个PostAdapter
(就像在大多数社交媒体应用程序中一样显示与帖子相关的数据),这个适配器有一个TextView
代表关注者数量的。
所以,导航结构可能是这样的:
PostAdpater
---(点击TextView
)--->FollowersActivity
通过单击TextView
代表关注者数量的图标,我们会转到FollowersActivity
,您可能会在此处显示关注者的详细信息。但问题是,您FollowersActivity
在PostAdapter
.
我能想出的唯一简单的解决方案是,您将查询函数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)
请告诉我,如果没有正确理解问题,我可能会想出更好的解决方案
推荐阅读
- forms - Xamarin 表单列表视图中的 TapGestureRecognition
- intellij-idea - 如何控制 (Java) lambdas 的 IntelliJ Idea 自动格式化括号位置?
- c# - 在统一独立构建中获取构建日期
- c# - 从 C# 使用时 F# 记录类型的相等性
- c# - 实体框架 - 在包含后使用 where() 过滤行
- firebase - Flutter Firebase Crashlytics 将堆栈跟踪报告为 Java 类文件而不是 Dart 文件
- node.js - 当 NPM 出现故障时,有什么方法可以避免停机?(在这种情况下与 GC App Engine 部署有关)
- django - Django 连接到 IBM Cloud 数据库
- javascript - 对象数组如何检查深度嵌套的文本字符串重复项并从数组中删除?
- react-native - Emailvalidator (require("email-validator") 停止工作