FireBaseRecyclerView in FirebaseRecyclerView can not be applied to : (arguments)

陌路散爱 提交于 2019-12-23 04:14:06

问题


I'm trying to retrieve data from Firebase to Recyclerview. I've already implement all other steps but now i'm getting error at the last stage when i'm implementing the adapter(FirebaseRecyclerAdapter).

My Fragment

public class Journal extends Fragment {

RecyclerView recyclerView;
DatabaseReference myref;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_journal,container,false);
    myref= FirebaseDatabase.getInstance().getReference("/Journal");
    recyclerView = (RecyclerView) v.findViewById(R.id.journal_rv);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    FirebaseRecyclerAdapter<JournalEntry, JournalHolder> adapter = new FirebaseRecyclerAdapter<JournalEntry, JournalHolder>(
            JournalEntry.class,
            R.layout.journal_list_item,
            JournalHolder.class,
            myref) {
        @Override
        protected void onBindViewHolder(JournalHolder holder, int position, JournalEntry model) {
            holder.setTitle(model.getEvent_title());
            holder.setContent(model.getEvent_content());
            holder.setSchool(model.getSchool_name());
            holder.setDate(model.getDate());
            holder.setStudents(model.getNumber_of_students());
            holder.setSchoolIv(model.getImg_url());
        }

        @Override
        public JournalHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }
    };

return  v;
}
public static class JournalHolder extends RecyclerView.ViewHolder{
    TextView Jtitle , Jcontent,Jschool,Jstudents,jdate;
    ImageView schoolIV;
    public JournalHolder(View itemView) {
        super(itemView);
        Jtitle =(TextView)itemView.findViewById(R.id.journal_title);
        Jcontent =(TextView)itemView.findViewById(R.id.event_content);
        Jschool =(TextView)itemView.findViewById(R.id.journal_school);
        Jstudents =(TextView)itemView.findViewById(R.id.journal_students);
        jdate =  (TextView)itemView.findViewById(R.id.journal_dates) ;
        schoolIV = (ImageView)itemView.findViewById(R.id.journal_image);





    }

    public void setTitle(String title) {
        Jtitle.setText(title);
    }

    public void setContent(String content) {
        Jcontent.setText(content);
    }

    public void setSchool(String school) {
        Jschool.setText(school);
    }

    public void setDate(String date) {
        jdate.setText(date);
    }

    public void setStudents(String students) {
        Jstudents.setText(students);
    }

    public void setSchoolIv(String schoolIv) {
        Picasso.with(itemView.getContext())
                .load(schoolIv)
                .into(schoolIV);
    }
}
}

This is what is shown as error

FireBaseRecyclerView in FirebaseRecyclerView can not be applied to :
 JournalEntry.class  (java...consti.last.database.JournalEntry>)

R.layout.journal_list_item  (int)
BlogViewHolder.class  (java...consti.last.Journal.BlogViewHolder>)
myref  (com...firebase.database.DatabaseReference)

Please help me. I tried all the methods but nothing works. Or if you have any advice...thank you in advance.

PS : I'm using Fragments


回答1:


under Activity declaration write:-

  private FirebaseRecyclerAdapter<JournalEntry, JournalHolder> adapter;

then inside onCreateView():

adapter = new FirebaseRecyclerAdapter<JournalEntry, JournalHolder>(
        JournalEntry.class,
        R.layout.journal_list_item,
        JournalHolder.class,
        myref) {   //be sure that these classes and parameters are correct..

above return v;:

     recyclerView.setAdapter(adapter);

Use this method instead of onBindViewHolder:

 @Override
        protected void populateViewHolder(JournalHolder holder,JournalEntry model, int position) {

Be sure you have all the correct methods in the model class JournalEntry.class and holder class JournalHolder.class and that its the right location and right layout.




回答2:


I finally found solution here:Firebase RecyclerView




回答3:


I had the same problem. In this version FirebaseResycleView use the following:

  Query query = FirebaseDatabase.getInstance()
            .getReference(); // you can add child as .child("child_name");


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

  FirebaseRecyclerAdapter<JournalEntry, JournalHolder> adapter = new FirebaseRecyclerAdapter<JournalEntry, JournalHolder>(
        options) {
    @Override
    protected void onBindViewHolder(JournalHolder holder, int position, JournalEntry model) {
        holder.setTitle(model.getEvent_title());
        holder.setContent(model.getEvent_content());
        holder.setSchool(model.getSchool_name());
        holder.setDate(model.getDate());
        holder.setStudents(model.getNumber_of_students());
        holder.setSchoolIv(model.getImg_url());
    }

    @Override
    public JournalHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.journal_list_item, parent, false);
            return new JournalHolder(view);
    }
};

And after don't forget:

    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setAdapter(adapter);
    adapter.startListening();


来源:https://stackoverflow.com/questions/47061142/firebaserecyclerview-in-firebaserecyclerview-can-not-be-applied-to-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!