我从不到3个月前开始学习教程,所以我假设它已经过时了。我在Android项目中使用FirebaseRecyclerAdapter
。目前,我正在尝试搜索存储在Firebase
中的图书,并使用searchBook
以我的FirebaseRecyclerAdapter
方法返回匹配的图书。但是,当我声明我的FirebaseRecyclerAdapter = new Firebas...
时,我被迫实施了两种方法onBindviewHolder
和onCreateViewHolder
。我不认为我需要这些,或者如果我这样做,有人可以向我解释我为什么会这样做吗?
目前这是我的代码:
private void searchBook(){
FirebaseRecyclerAdapter<Book, BookViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Book, BookViewHolder>(
Book.class,
R.layout.listview_booksearchresults,
BookViewHolder.class,
bookDatabase
) {
protected void populateViewHolder(BookViewHolder viewHolder, Book model, int position){
viewHolder.setDetails(model.getTitle(), model.getAuthor(), model.getImageURL());
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
}
如果我点击实现这两种方法,我的代码会变得错误,就像没有明天一样。
实施方法后的代码:
private void searchBook(){
FirebaseRecyclerAdapter<Book, BookViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Book, BookViewHolder>(
Book.class,
R.layout.listview_booksearchresults,
BookViewHolder.class,
bookDatabase
) {
@NonNull
@Override
public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
@Override
protected void onBindViewHolder(@NonNull BookViewHolder holder, int position, @NonNull Book model) {
}
protected void populateViewHolder(BookViewHolder viewHolder, Book model, int position){
viewHolder.setDetails(model.getTitle(), model.getAuthor(), model.getImageURL());
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
}
这是我第一次使用RecyclerAdapter
,所以我对这个概念非常陌生。希望这只是一个菜鸟错误!
我哪里出错了,为什么?
这是我更新的onCreate方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_searchbook);
bookDatabase = FirebaseDatabase.getInstance().getReference("All_Books");
searchField = (EditText) findViewById(R.id.book_Title);
search = (Button) findViewById(R.id.searchBookButton);
mResultList = (RecyclerView) findViewById(R.id.result_List);
mResultList.setHasFixedSize(true);
mResultList.setLayoutManager(new LinearLayoutManager(this));
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("All_Books");
Query query = ref.orderByChild("title").equalTo(searchField.toString());
FirebaseRecyclerOptions<Book> options =
new FirebaseRecyclerOptions.Builder<Book>()
.setQuery(query, Book.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Book, BookViewHolder>(options) {
@Override
public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_booksearchresults, parent, false);
return new BookViewHolder(view);
}
@Override
protected void onBindViewHolder(BookViewHolder holder, int position, Book model) {
holder.setDetails(model.getTitle(), model.getAuthor(), model.getImageURL());
}
};
mResultList.setAdapter(adapter);
}
答案 0 :(得分:1)
您使用的是最新的Firebaseui版本,不再有<button ion-button color="newcolor">Secondary</button>
,您需要执行以下操作:
首先,通过构建populateViewHolder
来配置适配器。 FirebaseRecyclerOptions
是您的POJO类,然后查询将是应该在recyclerview中显示的数据。
Book
然后创建DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("users");
Query query=ref.orderByChild("name").equalTo(name);
FirebaseRecyclerOptions<Book> options =
new FirebaseRecyclerOptions.Builder<Book>()
.setQuery(query, Book.class)
.build();
对象:
FirebaseRecyclerAdapter
更多信息: