RecyclerView onCreateViewHolder无法运行

时间:2017-11-21 03:23:45

标签: android android-recyclerview firebaseui

我已经搜索了几个小时,我确信它很简单,但我找不到答案。 onBindViewHolderpublic class MainActivity extends AppCompatActivity { private RecyclerView mBlogList; private DatabaseReference mDatabase; private Query query = FirebaseDatabase.getInstance().getReference().child("Blog").limitToLast(50); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog"); // query = FirebaseDatabase.getInstance().getReference().child("Blog").limitToLast(50); mBlogList = findViewById(R.id.blog_list); mBlogList.setHasFixedSize(true); mBlogList.setLayoutManager(new LinearLayoutManager(this)); //sets the orientation to vertical } @Override protected void onStart() { super.onStart(); Toast.makeText(this, "1", Toast.LENGTH_SHORT).show(); FirebaseRecyclerOptions<Blog> options = new FirebaseRecyclerOptions.Builder<Blog>() .setQuery(query, Blog.class) .build(); Toast.makeText(this, "2", Toast.LENGTH_SHORT).show(); FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(options) { @Override public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.blog_row, parent, false); Toast.makeText(MainActivity.this, "oh oh", Toast.LENGTH_SHORT).show(); return new BlogViewHolder(view); } @Override protected void onBindViewHolder(BlogViewHolder holder, int position, Blog model) { holder.setTitle(model.getTitle()); holder.setDesc(model.getDesc()); Toast.makeText(MainActivity.this, "oh si", Toast.LENGTH_SHORT).show(); } }; mBlogList.setAdapter(firebaseRecyclerAdapter); } public static class BlogViewHolder extends RecyclerView.ViewHolder { View mView; public BlogViewHolder(View itemView) { super(itemView); mView = itemView; } public void setTitle(String title) { TextView post_title = mView.findViewById(R.id.post_title); post_title.setText(title); } public void setDesc(String desc) { TextView post_desc = mView.findViewById(R.id.post_desc); post_desc.setText(desc); } } @Override public boolean onCreateOptionsMenu (Menu menu){ getMenuInflater().inflate(R.menu.main_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected (MenuItem item){ if (item.getItemId() == R.id.action_add) { startActivity(new Intent(getApplicationContext(), Postctivity.class)); } return super.onOptionsItemSelected(item); } } 未运行。有人可以帮我吗。感谢

MainActivity.java

public class Blog {

private String title;
private String desc;
private String image;

public Blog(){

}

public Blog(String title, String desc, String image) {
    this.title = title;
    this.desc = desc;
    this.image = image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}
}

Blog.class

public class Blog {
private String title;
private String desc;
private String image;

public Blog(){

}

public Blog(String title, String desc, String image) {
    this.title = title;
    this.desc = desc;
    this.image = image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}
}

blog_row.xml

navigateWebsite()

2 个答案:

答案 0 :(得分:0)

根据the documentation for the library you're using,您必须致电adapter.startListening()以实际读取数据库中的信息。

  

FirebaseRecyclerAdapter使用事件监听器来监控Firebase查询的更改。要开始侦听数据,请调用startListening()方法。您可能希望在onStart()方法中调用此方法。确保在调用startListening()之前已完成读取数据所需的任何身份验证,否则您的查询将失败。

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

看起来您已经在onStart()中进行了适配器的所有设置,所以我认为您最后可以添加一行:

firebaseRecyclerAdapter.startListening();

答案 1 :(得分:0)

Noob试图在这里学习。 recyclerView没有从firebase数据库下载内容。我做了什么来修复它是从setHasFixedSize(true)更改为setHasFixedSize(false)我不知道为什么它修复它但确实如此。我知道我的问题很混乱我的英语不是很好。我希望这有帮助。