gpt4 book ai didi

java - 没有为 FirebaseRecyclerAdapter<> 调用 OnCreateViewHolder

转载 作者:太空宇宙 更新时间:2023-11-04 10:57:10 25 4
gpt4 key购买 nike

我尝试调试我的程序,发现执行没有在 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 个小时想知道我做错了什么......好吧,事情是这样的。

希望有帮助。

关于java - 没有为 FirebaseRecyclerAdapter<> 调用 OnCreateViewHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47247345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com