没有为FirebaseRecyclerAdapter<>

时间:2017-11-12 09:41:16

标签: java android android-layout

我尝试调试我的程序并注意到FirebaseRecyclerAdapter内部没有执行recyclelerAdapter = new FirebaseRecyclerAdapter(                 选项)。我已经尝试了所有可能的解决方案stackoverflow提供但似乎没有解决我的问题。我附加我的MainActivity.java,activity_main.xml,如果你需要任何其他附件,那么告诉我, 谢谢

MainActivity.java

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private DatabaseReference myref;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(false);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    myref= FirebaseDatabase.getInstance().getReference().child("/blog");
    FirebaseRecyclerOptions<Blog> options =
            new FirebaseRecyclerOptions.Builder<Blog>()
                    .setQuery(myref, Blog.class)
                    .build();

    FirebaseRecyclerAdapter<Blog,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<Blog,BlogViewHolder>(
            options) {
         //this is not being executed
@Override
        public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.individual_row, parent, false);

            return new BlogViewHolder(view);
        }

        @Override
        public void onBindViewHolder(BlogViewHolder holder, int position, Blog model) {
            holder.setTitle(model.getTitle());
            holder.setDescription(model.getDescription());
            holder.setImage(model.getImage());
        }
    };
    recyclerView.setAdapter(recyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
    View mView;
    TextView textView_title;
    TextView textView_decription;
    ImageView imageView;
    public BlogViewHolder(View itemView) {
        super(itemView);
        mView=itemView;
        textView_title = (TextView)itemView.findViewById(R.id.title);
        textView_decription = (TextView) itemView.findViewById(R.id.description);
        imageView=(ImageView)itemView.findViewById(R.id.image);
    }
    public void setTitle(String title)
    {
        textView_title.setText(title+"");
    }
    public void setDescription(String description)
    {
        textView_decription.setText(description);
    }
    public void setImage(String image)
    {
        Picasso.with(mView.getContext())
                .load(image)
                .into(imageView);
    }
}
}

activity_main.xml中

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 >


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 </android.support.v7.widget.RecyclerView>


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

抱歉迟到了,我希望你有同样的错误,你错过的是这个:

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

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

我刚刚失去了2个小时,想知道我做错了什么......好吧,就像这样。

希望它有所帮助。