Firebase FirebaseRecyclerAdapter无法正常工作

时间:2017-11-01 12:30:35

标签: android firebase firebase-realtime-database

我正在尝试使用firebase内容填充recyclerView,但我正在捕捉错误。 这是我的活动片段

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


    recyclerView=(RecyclerView)v.findViewById(R.id.journal_rv);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    myref= FirebaseDatabase.getInstance().getReference().child("/blog");

  // here is the error : "Must implements methods"



    FirebaseRecyclerAdapter<JournalEntry,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<JournalEntry, BlogViewHolder>(
            JournalEntry.class,
            R.layout.journal_list_item,
            BlogViewHolder.class,
            myref
    ) {


// Another one : method does not override from its superclass


        @Override
        protected void populateViewHolder(BlogViewHolder viewHolder, JournalEntry model, int position) {
            viewHolder.setTitle(model.getTitle());
            viewHolder.setDescription(model.getDescription());

        }
    };
    recyclerView.setAdapter(recyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
    View mView;
    TextView textView_title;
    TextView textView_decription;
    public BlogViewHolder(View itemView) {
        super(itemView);
        mView=itemView;
        textView_title = (TextView)itemView.findViewById(R.id.journal_title);
        textView_decription = (TextView) itemView.findViewById(R.id.journal_event_title);
    }
    public void setTitle(String title)
    {
        textView_title.setText(title+"");
    }
    public void setDescription(String description)
    {
        textView_decription.setText(description);
    }

当我实现这些方法时,我总是遇到同样的错误;

谁能帮助我吗? Ps:我使用的是片段而不是活动。 更新我实现了像这样的方法

 FirebaseRecyclerAdapter<JournalEntry,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<JournalEntry, BlogViewHolder>(
            JournalEntry.class,
            R.layout.journal_list_item,
            BlogViewHolder.class,
            myref
    ) {
        @Override
        public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }

        @Override
        protected void onBindViewHolder(BlogViewHolder holder, int position, JournalEntry model) {
            holder.setTitle(model.getEvent_title());
            holder.setDescription(model.getEvent_content());
        }


    };

现在错误(内部编辑器)是firebaseAdapter中的FirebaseAdaper无法应用于:

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)

3 个答案:

答案 0 :(得分:0)

要解决此问题,您需要在onCreateView()方法的末尾添加以下代码行:

return v;

如果还有其他unimplemented methods,只需按下您的Android Studio ALT+Insert - &gt;选择覆盖方法 - &gt;选择嵌套方法。

答案 1 :(得分:0)

recyclerView.setHasFixedSize(true);

从代码中删除此行

答案 2 :(得分:-1)

我遇到了类似的问题,并删除了 recyclerView.setHasFixedSize(true);线,它的工作原理。我只是不明白为什么。请赐教