首页 > 解决方案 > 无法从 firebase 实时数据库中检索数据到 recyclerview

问题描述

我正在尝试从 firebase 实时数据库中检索数据到我的 recyclerview 。早些时候,我能够在我的 recyclerview 上检索数据,但不知道发生了什么,它突然开始向我显示错误。我在这一行得到错误

User user=snapshot.getValue(User.class);

而logcat窗口中的这个错误

com.google.firebase.database.DatabaseException:com.shivam.chatapp2.Model.User 类未定义无参数构造函数。如果您使用 ProGuard,请确保未剥离这些构造函数。在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize

我的完整代码

用户适配器.java:

    public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {

    private Context mContext;
    private List<User> mUsers;

    public UserAdapter(Context mContext, List<User> mUsers) {
        this.mContext = mContext;
        this.mUsers = mUsers;
    }

    @NonNull
    @Override
    public UserAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.user_item,parent,false);

        return new UserAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UserAdapter.ViewHolder holder, int position) {


        final User user=mUsers.get(position);
        holder.username.setText(user.getFirst());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, MessageActivity.class);
                intent.putExtra("UserName",user.getFirst());
                intent.putExtra("userid", user.getId());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mUsers.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView username;
        public ImageView profile_image;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            username=itemView.findViewById(R.id.username);
            profile_image=itemView.findViewById(R.id.profile_image);
        }
    }
}

用户.java:

public class User {

    private String id;
    private String First;
    private String Last;
    private String EmailID;

    public User(String id, String first, String last, String emailID) {
        this.id = id;
        First = first;
        Last = last;
        EmailID = emailID;
    }


    public User(String userid, String first_name, String eMail) {

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirst() {
        return First;
    }

    public void setFirst(String first) {
        First = first;
    }

    public String getLast() {
        return Last;
    }

    public void setLast(String last) {
        Last = last;
    }

    public String getEmailID() {
        return EmailID;
    }

    public void setEmailID(String emailID) {
        EmailID = emailID;
    }

}

用户片段.java:

    public class UsersFragment extends Fragment {
    private RecyclerView recyclerView;
    private UserAdapter mUserAdapter;
    private List<User> mUsers;
    String TAG = "MyTag";

    public UsersFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_users, container, false);
        recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mUsers = new ArrayList<>();

        readUser();


        return view;
    }

    private void readUser() {

        final FirebaseUser firebaseUser=FirebaseAuth.getInstance().getCurrentUser();
        DatabaseReference reference= FirebaseDatabase.getInstance().getReference("Users");

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mUsers.clear();
                for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
                    User user=snapshot.getValue(User.class);
                    mUsers.add(user);


                }
                mUserAdapter=new UserAdapter(getContext(),mUsers);
                recyclerView.setAdapter(mUserAdapter);
                mUserAdapter.notifyDataSetChanged();

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }
}

标签: androidfirebasefirebase-realtime-database

解决方案


根据您的错误日志,它指出:

User 类没有在 User.class 文件中声明的默认构造函数。

在 User.class 文件中为 User 类添加一个默认构造函数,例如:

 public User()
    {

      }

推荐阅读