我尝试调试程序,并注意到FirebaseRecyclerAdapter recyclerAdapter = 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>
最佳答案
抱歉,我迟到了,希望您也遇到了同样的错误,这是您所缺少的:
@Override
public void onStart() {
super.onStart();
recyclerAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
recyclerAdapter.stopListening();
}
我只是迷失了2个小时,不知道自己在做什么错。。。嗯,就像这样。
希望能帮助到你。