无法从Firebase获取数据

时间:2019-05-04 09:47:48

标签: android firebase android-adapter

我不知道我的代码出了什么问题,但是我无法获得理想的结果。要么数据没有到来,要么数据无法正确显示,我在代码中找不到问题。数据库模型文件中没有设置器,但我已附加适配器

适配器文件


          public class BloodRequestAdapter extends   RecyclerView.Adapter<BloodRequestAdapter.MyViewHolder> {

    Context context;
    ArrayList<BloodRequestList> bloodRequestList;

    public BloodRequestAdapter(Context c, ArrayList<BloodRequestList> b){
        context=c;
        bloodRequestList=b;
    }
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.user,viewGroup,false));
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {

            myViewHolder.name.setText(bloodRequestList.get(i).getPatientName());
            myViewHolder.bloodgroup.setText(bloodRequestList.get(i).getBloodGroup());
            myViewHolder.quantity.setText(bloodRequestList.get(i).getQuantity());
            myViewHolder.phone.setText(bloodRequestList.get(i).getPhone());
            myViewHolder.hospitalname.setText(bloodRequestList.get(i).getHospitalName());
            myViewHolder.hospitaladdress.setText(bloodRequestList.get(i).getHospitalAddress());

        }

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

        class MyViewHolder extends RecyclerView.ViewHolder{

            TextView name,bloodgroup,quantity,phone,hospitalname,hospitaladdress;
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                name=itemView.findViewById(R.id.textViewNameFeed);
                bloodgroup=itemView.findViewById(R.id.textViewBloodtypeFeed);
                quantity=itemView.findViewById(R.id.textViewquantityFeed);
                phone=itemView.findViewById(R.id.textViewPhoneFeed);
                hospitalname=itemView.findViewById(R.id.textViewHospitalNameFeed);
                hospitaladdress=itemView.findViewById(R.id.textViewHospitalAddressFeed);
            }
        }
    }

这是具有数据库引用以及附加适配器类的主要片段



      public class FeedFragment extends Fragment {

        View view;
        DatabaseReference databaseReference;
        RecyclerView recyclerView;
        ArrayList<BloodRequestList> list;
        BloodRequestAdapter adapter;

        public FeedFragment() {

        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.feed_fragment, container, false);


        databaseReference=FirebaseDatabase.getInstance().getReference().child("bloodRequest");

        recyclerView=view.findViewById(R.id.myRecycler);
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            list=new ArrayList<>();
            databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange (@NonNull DataSnapshot dataSnapshot){
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    BloodRequestList bloodRequestList = dataSnapshot1.getValue(BloodRequestList.class);
                    list.add(bloodRequestList);
                }
                adapter = new BloodRequestAdapter(getActivity(), list);
                recyclerView.setAdapter(adapter);

            }

            @Override
            public void onCancelled (@NonNull DatabaseError databaseError){
                Toast.makeText(getActivity(), "somethings wrong", Toast.LENGTH_LONG).show();
            }
        });

            return view;
    }
    }

模型文件

 public class BloodRequestList {
    String PatientName;
    String BloodGroup;
    String Quantity;
    String Phone;
    String HospitalName;
    String HospitalAddress;

    public BloodRequestList() {
    }

    public BloodRequestList(String patientName, String bloodGroup, String quantity, String phone, String hospitalName, String hospitalAddress) {
        PatientName = patientName;
        BloodGroup = bloodGroup;
        Quantity = quantity;
        Phone = phone;
        HospitalName = hospitalName;
        HospitalAddress = hospitalAddress;

    }
        public String getPatientName() {
            return PatientName;
        }

        public String getBloodGroup() {
            return BloodGroup;
        }

        public String getQuantity() {
            return Quantity;
        }

        public String getPhone() {
            return Phone;
        }

        public String getHospitalName() {
            return HospitalName;
        }

        public String getHospitalAddress() {
            return HospitalAddress;
        }

    }

数据库



      {
         "bloodRequest" : 
          {
        "986153834" : 
           {
          "bloodGroup" : "A+",

                  "hospitalAddress" : "sjvkqk",

                  "hospitalName" : "jshd",

                  "patientName" : "rjwk",

                  "phone" : "986153834",

                  "quantity" : "4"

           }
        }
    }

Logcat


    05-04 15:04:10.491 999-999/com.example.bloodbank E/RecyclerView: No adapter attached; skipping layout
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for quantity found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for phone found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for bloodGroup found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for hospitalName found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for hospitalAddress found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for patientName found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for quantity found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for phone found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for bloodGroup found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for hospitalName found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for hospitalAddress found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.511 999-999/com.example.bloodbank W/ClassMapper: No setter/field for patientName found on class com.example.bloodbank.BloodRequestList (fields/setters are case sensitive!)
05-04 15:04:10.516 999-999/com.example.bloodbank D/TextView: setTypeface with style : 0

1 个答案:

答案 0 :(得分:0)

请输入以下代码:

 @Override
    protected void setupDB() {
        dbRef = FirebaseDatabase.getInstance().getReference(getResources().getString(R.string.db_name));
        getData("BloodRequestList.class");
    }