将firebase数据库记录获取到RecyclerView

时间:2018-05-04 03:18:47

标签: android firebase firebase-realtime-database

我最近在学习firebase数据库。现在我正在尝试将记录放到RecyclerView

这是我做的事情

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());
        }
    });

这是我的适配器

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();
    }
}

这是我的模特

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;
    }
}

我收到此错误

  

无法将java.lang.String类型的对象转换为类型   ZZZZ.BiodataModel

我该如何解决?

1 个答案:

答案 0 :(得分:1)

您的参考是:

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

所以在孩子Biodata,然后你在它的直接孩子for (DataSnapshot postSnapshot : dataSnapshot.getChildren())内进行迭代,从而检索字符串AlamatEmail

要解决此问题,您需要删除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());
            }
        }
    }