DataSnapshot 无法转换为适配器 [关闭]
Posted
技术标签:
【中文标题】DataSnapshot 无法转换为适配器 [关闭]【英文标题】:DataSnapshot cannot be cast to Adapter [closed] 【发布时间】:2021-12-05 11:28:58 【问题描述】:我有一个问题,我正在为 RecyclerView 创建一个适配器,为了创建它,我使用 FirebaseRecyclerAdapter 扩展了这个类,但是我得到一个错误,我不知道如何解决它,也许有人可以告诉我一些事情.我在网上找到了一个类似的例子,可以通过这种方式扩展一个类,但是要么我做错了,要么不再可以以这种方式扩展类。非常感谢您的帮助
这是我的适配器:
public class PostAdapterTest extends FirebaseRecyclerAdapter <Posts, PostAdapterTest.postsViewHolder> //error the error is here
private BottomSheetDialog bottomSheetDialog;
private Context mContext;
private Boolean downloadOrOpen = false;
private boolean buttonStatus = true;
private DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference("Likes");
private FirebaseUser mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
private String difficulty = null;
private int likesCounter;
private ReadWritePermissions mReadWritePermissions;
public PostAdapterTest(@NonNull FirebaseRecyclerOptions<Posts> postsFirebaseRecyclerOptions, ReadWritePermissions mReadWritePermissions, Context mContext)
super(postsFirebaseRecyclerOptions);
this.mContext = mContext;
this.mReadWritePermissions = mReadWritePermissions;
@Override
protected void onBindViewHolder(@NonNull PostAdapterTest.postsViewHolder holder, int position, @NonNull Posts model)
final String postKey = getRef(position).getKey();
String postUid = model.getPostId();
holder.nickname.setText(model.getNickname());
holder.country.setText(model.getCountry());
holder.title.setText(model.getCountry());
holder.days.setText(model.getDays());
holder.kilometers.setText(model.getKilometers());
holder.description.setText(model.getDescription());
difficulty = model.getDifficulty();
holder.map.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Uri mapUri = Uri.parse(model.getMap());
mapOptions(mapUri);
);
holder.comments.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
CommentsFragment commentsFragment = new CommentsFragment();
Bundle args = new Bundle();
args.putString("postId", postUid);
commentsFragment.setArguments(args);
FragmentTransaction fragmentTransaction = ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_container, commentsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
);
holder.setLikeButtonStatus(postKey);
holder.likes.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
buttonStatus = true;
likesRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
if(buttonStatus == true)
if(snapshot.child(postKey).hasChild(mFirebaseUser.getUid()))
likesRef.child(postKey).child(mFirebaseUser.getUid()).removeValue();
buttonStatus = false;
else
likesRef.child(postKey).child(mFirebaseUser.getUid()).setValue(true);
buttonStatus = false;
@Override
public void onCancelled(@NonNull DatabaseError error)
);
switch (difficulty)
case "Easy":
holder.difficulty.setImageResource(R.drawable.ic_easy);
break;
case "Difficult":
holder.difficulty.setImageResource(R.drawable.ic_difficult);
break;
case "Hard":
holder.difficulty.setImageResource(R.drawable.ic_hard);
break;
ArrayList<Slider> sliderArrayList = new ArrayList<>();
Picasso.get().load(model.getAvatarUri()).into(holder.avatarUri);
);
@NonNull
@Override
public PostAdapterTest.postsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.posts, parent, false);
return new PostAdapterTest.postsViewHolder(view);
class postsViewHolder extends RecyclerView.ViewHolder
public CircleImageView avatarUri;
public TextView nickname, country, title, days, likesNum, kilometers,description, fileSize, fileName;
public SliderView mSliderView;
public ImageView comments, save, difficulty;
public ImageButton likes;
public ImageButton more;
public LinearLayout map;
public postsViewHolder(@NonNull View itemView)
super(itemView);
avatarUri = itemView.findViewById(R.id.posts_profile_picture);
nickname = itemView.findViewById(R.id.posts_nickname);
country = itemView.findViewById(R.id.posts_country);
title = itemView.findViewById(R.id.posts_title);
days = itemView.findViewById(R.id.posts_days);
difficulty = itemView.findViewById(R.id.posts_difficulty);
mSliderView = itemView.findViewById(R.id.posts_images);
likes = itemView.findViewById(R.id.posts_likes);
comments = itemView.findViewById(R.id.posts_comments);
save = itemView.findViewById(R.id.posts_save);
likesNum = itemView.findViewById(R.id.posts_likes_number);
description = itemView.findViewById(R.id.posts_description);
more = itemView.findViewById(R.id.posts_more);
kilometers = itemView.findViewById(R.id.posts_distance);
fileSize = itemView.findViewById(R.id.posts_uploaded_file_size);
fileName = itemView.findViewById(R.id.posts_uploaded_file_name);
map = itemView.findViewById(R.id.posts_map_layout);
public void setLikeButtonStatus(final String key)
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
likesRef.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
likesCounter = (int)snapshot.child(key).getChildrenCount();
if(snapshot.child(key).hasChild(mFirebaseUser.getUid()))
likes.setImageResource(R.drawable.ic_liked_red);
likesNum.setText(likesCounter + " Likes");
else if(likesCounter == 0)
likes.setImageResource(R.drawable.ic_unliked_red);
likesNum.setText("");
else
likes.setImageResource(R.drawable.ic_unliked_red);
likesNum.setText(likesCounter + " Likes");
@Override
public void onCancelled(@NonNull DatabaseError error)
);
private void mapOptions(Uri mapUri)
Logcat:
Process: com.company.android.myapplication, PID: 8349
java.lang.ClassCastException: com.google.firebase.database.DataSnapshot cannot be cast to com.company.android.myapplication.Posts
at com.company.android.myapplication.test.PostAdapterTest.onBindViewHolder(PostAdapterTest.java:44)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
【问题讨论】:
您在哪一行代码中遇到了这个错误? @AlexMamo 适配器类的第一行我在那里写了评论 您的标题与堆栈跟踪不匹配 【参考方案1】:java.lang.ClassCastException:com.google.firebase.database.DataSnapshot 无法转换为 com.company.android.myapplication.Posts
您不能将DataSnapshot
转换为您自己的pojo Posts
,您必须从DataSnapshot
获取value
。此外,请确保 DataSnapshot
架构和您的 pojo Posts
架构正确匹配,以将 DataSnapshot
值解析为 Posts
。
另外,直接从适配器进行查询或调用活动或进行 Firebase 事务是非常糟糕的方法。您应该将回调传递给适配器并处理片段/活动的逻辑。
【讨论】:
你能给我举个例子吗? 帖子 post = (Posts) postSnapshot.child("posts").getValue(); 同样的问题是我无法完成类或活动中的所有逻辑,因为我有几个活动需要填充 RecyclerView,因此我得到了重复的代码 那么您应该找到更好的方法来满足您的要求。以上是关于DataSnapshot 无法转换为适配器 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
将 DataSnapshot 转换为自定义结构对象 - Swift 4
如何将 firbease 的 DataSnapshot 转换为 json 文件或如何将 firebase 数据库检索为 json 文件
从 '(_) throws -> ()' 类型的抛出函数到非抛出函数类型 '(DataSnapshot) -> Void' 的无效转换
如何从 Android 上的 Firebase DataSnapshot 中提取对象列表