简体   繁体   English

获取 Firebase 数据库记录到 RecyclerView

[英]Get firebase database records to RecyclerView

I'm recently learning about firebase database.我最近正在学习 firebase 数据库。 Now I'm trying to put the records to RecyclerView现在我正在尝试将记录放入RecyclerView

Here is what I do这是我所做的

BiodataModel value;
DatabaseReference mdatabase;
private List<BiodataModel> mBiodata;
BiodataAdapter dAdapter;
List<BiodataModel> userlist = new ArrayList<>();

    mdatabase = FirebaseDatabase.getInstance().getReference().child("Biodata");
    mdatabase.keepSynced(true);

    mdatabase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            userlist = new ArrayList<>();
            if (dataSnapshot != null && dataSnapshot.getValue() != null) {
                try {
                    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                        BiodataModel bioModel = postSnapshot.getValue(BiodataModel.class); // The problem is here
                        userlist.add(bioModel);
                    }

                } catch (Exception ex) {
                    Log.e("MYERROR", ex.getMessage());
                }
            }
        }



        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.d("MYERROR", databaseError.getMessage());
        }
    });

Here is my adapter这是我的适配器

public  class BiodataAdapter extends RecyclerView.Adapter<BiodataAdapter.MyViewHolder>{
    private List<BiodataModel> bioList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView fullname;

        public MyViewHolder(View view) {
            super(view);
            fullname = (TextView) view.findViewById(R.id.fullname);

        }
    }

    public BiodataAdapter(){

    }

    public BiodataAdapter(List<BiodataModel> bioList) {
        this.bioList = bioList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.biodata_list_row, parent, false);

        return new MyViewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        BiodataModel bio = bioList.get(position);
        holder.fullname.setText(bio.getFullname());
    }

    @Override
    public int getItemCount() {
        return bioList.size();
    }
}

and this is my Model这是我的模型

public class BiodataModel {

    public String fullname,email,noTelp,alamat;

    public BiodataModel(String fullname, String email, String noTelp,String alamat){
        this.fullname = fullname;
        this.email = email;
        this.noTelp = noTelp;
        this.alamat = alamat;
    }
    public String getFullname() {
        return fullname;
    }

    public String getEmail() {
        return email;
    }

    public String getNoTelp() {
        return noTelp;
    }

    public String getAlamat() {
        return alamat;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setNoTelp(String noTelp) {
        this.noTelp = noTelp;
    }

    public void setAlamat(String alamat) {
        this.alamat = alamat;
    }
}

and i get this error我收到这个错误

Can't convert object of type java.lang.String to type ZZZZ.BiodataModel无法将 java.lang.String 类型的对象转换为 ZZZZ.BiodataModel 类型

how can i fix it ?我该如何解决?

Your reference is this:你的参考是这样的:

 mdatabase = FirebaseDatabase.getInstance().getReference().child("Biodata");

so at child Biodata , then you are iterating inside it's direct child for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) , thus retrieving the Strings Alamat , Email .所以在 child Biodata ,然后你在它的直接孩子内部迭代for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) ,从而检索 Strings Alamat , Email

To solve this, you need to remove the for loop and try the following:要解决此问题,您需要删除 for 循环并尝试以下操作:

mdatabase.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        userlist = new ArrayList<>();
        if (dataSnapshot != null && dataSnapshot.getValue() != null) {
            try {

                    BiodataModel bioModel = dataSnapshot.getValue(BiodataModel.class);
                    userlist.add(bioModel);


            } catch (Exception ex) {
                Log.e("MYERROR", ex.getMessage());
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM