无法将java.lang.String类型的对象转换为com.example.messenger.messenger.Friends类型

时间:2018-09-29 09:45:00

标签: android firebase firebase-realtime-database

我想将Firebase数据显示到RecyclerView中。 Firebase由DatabaseReference“ Friends”组成,并具有用户ID,如下面的图片所示,
enter image description here

我想通过此代码获取特定ID的数据

mAuth = FirebaseAuth.getInstance();

mCurrent_user_id = mAuth.getCurrentUser().getUid();
queryFriends = FirebaseDatabase.getInstance().getReference().child("Friends").child(mCurrent_user_id);

在上面的行中,.child(mCurrent_user_id)创建致命错误,应用崩溃了。我已经通过以下代码记录了mCurrent_user_id,这些代码给出了与 Friends 数据库参考中相同的正确结果。

Log.d("sdfsdf", mCurrent_user_id);

日志结果为com.example.messenger.messenger D/sdfsdf: wed5qPTCdcQVzVlRcBrMo1NX43v1

回收站代码:

queryFriends = FirebaseDatabase.getInstance().getReference().child("Friends").child(mCurrent_user_id);
        queryFriends.keepSynced(true);

        FirebaseRecyclerOptions<Friends> options =
                new FirebaseRecyclerOptions.Builder<Friends>()
                        .setQuery(queryFriends, Friends.class)
                        .build();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(options) {
            @Override
            public FriendsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                // Create a new instance of the ViewHolder, in this case we are using a custom
                // layout called R.layout.message for each item
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_users_activity_layout, parent, false);

                return new FriendsViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(final FriendsViewHolder friendsViewHolder, int position, Friends friends) {
                friendsViewHolder.setDate(friends.getDate());
}

观看者:

public static class FriendsViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public FriendsViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }

        public void setDate(String date){
            TextView userStatusView = (TextView) mView.findViewById(R.id.user_single_status);
            userStatusView.setText(date);
        }

        public void setName(String name){
            TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
            userNameView.setText(name);
        }

例外:

E/AndroidRuntime: FATAL EXCEPTION: main
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.messenger.messenger.Friends
        at com.google.android.gms.internal.zzelw.zzb(Unknown Source)
        at com.google.android.gms.internal.zzelw.zza(Unknown Source)

规则: enter image description here

1 个答案:

答案 0 :(得分:1)

我相信这些行会引起问题:

userNameView.setText(name);
userStatusView.setText(date);

您的ViewHolder在将数据设置为视图时遇到问题。尝试使用onBindViewHolder()方法进行设置。

更新:以您的Viewholder的身份尝试以下操作:

public class FriendsViewHolder extends RecyclerView.ViewHolder {

    TextView textName;
    TextView textDate;

    public FriendsViewHolder(View itemView) {
        super(itemView);

        textName = itemView.findViewById(R.id.user_single_name);
        textDate = itemView.findViewById(R.id.user_single_status);


    }

}

onBindViewHolder()内:

friendsViewHolder.textName.setDate(friends.getName());
friendsViewHolder.textDate.setDate(friends.getDate());

我假设您的Adapter具有用于设置日期名称的设置器。