我在使用Firebase填充RecyclerView时遇到问题

时间:2019-10-16 10:04:50

标签: java android firebase firebase-realtime-database android-recyclerview

我在片段中使用了Firebasew和firebaseRecyclerAdapter来填充recyclerView,它只是行不通,我需要帮助。    当我运行代码时,出现错误消息:

  

在课堂上找不到设置者/领域,但我有设置者。

浏览以下片段的代码:

package com.example.android.voguetailors;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.android.voguetailors.Data.JobData;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.firebase.ui.database.SnapshotParser;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {
    private static final String TAG = HomeFragment.class.getSimpleName();
    private static final String URL = "my vogue tailors firebase maybe?";

    private EditText editText, etd, timePosted, photoUrl;
    private Button button;
    private RecyclerView recyclerView;
    private LinearLayoutManager mLayoutManager;


    //Firebase Instance Variables
    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference mJobDatabaseReference;
    private FirebaseRecyclerAdapter firebaseRecyclerAdapter;


    private Query mQueryCurrent;

    public HomeFragment() {
        // 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_home, container, false);

        editText = view.findViewById(R.id.et);
        etd = view.findViewById(R.id.etd);
        timePosted = view.findViewById(R.id.time_posted);
        photoUrl = view.findViewById(R.id.photo_url);
        button = view.findViewById(R.id.btn);
        mLayoutManager = new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_home_fragment);
        recyclerView.setLayoutManager(mLayoutManager);

        mJobDatabaseReference = FirebaseDatabase.getInstance().getReference().child("jobs");
        mJobDatabaseReference.keepSynced(true);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("jobs").push();
                Map<String, Object> map = new HashMap<>();
                //map.put("id", databaseReference.getKey());
                map.put("name", editText.getText().toString());
                map.put("job description", etd.getText().toString());
                map.put("timeposted", timePosted.getText().toString());
                map.put("photoUrl", photoUrl.getText().toString());

                databaseReference.setValue(map);

            }
        });

        fetch();

        return view;
    }


    @Override
    public void onStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        firebaseRecyclerAdapter.stopListening();
    }

    public static class JobDataViewHolder extends RecyclerView.ViewHolder {
        TextView mName, mDescription, mTimeposted;
        ImageView picture;

        public JobDataViewHolder(View view) {
            super(view);
            mName = view.findViewById(R.id.hospital_name);
            mDescription = view.findViewById(R.id.hospital_information);
            mTimeposted = view.findViewById(R.id.time_posted);
            picture = view.findViewById(R.id.hospital_picture);

        }

        public void setName(String name) {
            mName.setText(name);
        }

        public void setDescription(String description) {
            mDescription.setText(description);
        }

        public void setTimeposted(String timeposted) {
            mTimeposted.setText(timeposted);
        }

        public void setPicture(int pictureResourceId) {
            picture.setImageResource(pictureResourceId);
        }
    }

    private void fetch() {
        Query query = FirebaseDatabase.getInstance()
                .getReference()
                .child("jobs");

        FirebaseRecyclerOptions<JobData> options =
                new FirebaseRecyclerOptions.Builder<JobData>()
                        .setQuery(query, JobData.class)
                        .build();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<JobData, JobDataViewHolder>(options) {
            @NonNull
            @Override
            public JobDataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.homescreen_list_item, parent, false);
                return new JobDataViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(@NonNull JobDataViewHolder holder, int position, @NonNull JobData model) {

                holder.setName(model.getHospitalName());
                holder.setDescription(model.getJobDescription());
                holder.setTimeposted(model.getTimePosted());
                //holder.setPicture(model.getHospitalPhotoUrl());
            }
        };

        recyclerView.setAdapter(firebaseRecyclerAdapter);

    }
}

我具有fragment_home.xml设置的布局和homescreen_list_item的设置。

我正在尝试从我的Firebase数据库节点“作业”中检索数据

这是POJO类

package com.example.android.voguetailors.Data;

public class JobData {
    //basically a pojo for parsing json data from my firebase project :)
    private String mHospitalName;
    private String mJobDescription;
    private String mHospitalPhotoUrl;
    private String mTimePosted;

    public JobData(){
        //needed for Firebase
    }
    public JobData(String hospitalName, String jobDescription,  String timePosted, String hospitalPhoto){
        mHospitalName = hospitalName;
        mJobDescription = jobDescription;
        mTimePosted = timePosted;
        mHospitalPhotoUrl = hospitalPhoto;

    }

    public String getHospitalName() {
        return mHospitalName;
    }

    public void setHospitalName(String hospitalname) {
        this.mHospitalName = hospitalname;
    }

    public String getJobDescription() {
        return mJobDescription;
    }

    public void setJobDescription(String jobdescription) {
        this.mJobDescription = jobdescription;
    }
    public void setTimePosted (String timeposted){
        this.mTimePosted = timeposted;
    }
    public String getTimePosted(){
        return mTimePosted;
    }

    public String getHospitalPhotoUrl() {
        return mHospitalPhotoUrl;
    }

    public void setHospitalPhotoUrl(String mHospitalPhotoUrl) {
        this.mHospitalPhotoUrl = mHospitalPhotoUrl;
    }
}

这是JSON结构: enter image description here

这是不同的,在这里我使用FirebaseRecyclerAdapter。那与在其他问题中使用的不同。我尽量尝试他们的解决方案,但没有得到任何结果。谢谢。

0 个答案:

没有答案