FirebaseRecyclerView中的FireBaseRecyclerView无法应用于:(参数)

问题描述 投票:1回答:3

我正在尝试从Firebase检索数据到Recyclerview。我已经实现了所有其他步骤,但现在我在实现适配器(FirebaseRecyclerAdapter)的最后阶段遇到错误。

我的片段

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

这是显示为错误的内容

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)

请帮我。我尝试了所有的方法,但没有任何作用。或者如果您有任何建议......请提前感谢您。

PS:我正在使用Fragments That error is shown here

android firebase firebase-realtime-database firebaseui
3个回答
1
投票

在活动声明下写: -

  private FirebaseRecyclerAdapter<JournalEntry, JournalHolder> adapter;

然后在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..

以上return v;

     recyclerView.setAdapter(adapter);

使用此方法而不是onBindViewHolder:

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

确保你在模型类JournalEntry.class和holder class JournalHolder.class中拥有所有正确的方法,并且它的位置和正确的布局正确。


0
投票

我终于找到了解决方案:Firebase RecyclerView


0
投票

我有同样的问题。在此版本中FirebaseResycleView使用以下内容:

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

之后别忘了:

    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setAdapter(adapter);
    adapter.startListening();
© www.soinside.com 2019 - 2024. All rights reserved.