This question already has answers here:
firebaserecycleradapter() cannot be applied to FirebaseRecyclerAdapter
(5 answers)
Closed 3 years ago.
The same error keeps coming up. I'm guessing they changed how it works now but since I'm a noob, I really need help fixing it.
private void loadMenu() {
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>
(Category.class, R.layout.menu_item, MenuViewHolder.class, category) {
#NonNull
#Override
public MenuViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
#Override
protected void onBindViewHolder(#NonNull MenuViewHolder viewHolder, int position, #NonNull Category model) {
viewHolder.txtMenuName.setText(model.getNama());
Picasso.get().load(model.getImage()).into(viewHolder.imageView);
final Category clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
//get Category Id and Send to new Activity
Intent foodlist = new Intent(Home.this, FoodList.class);
foodlist.putExtra("CategoryId",adapter.getRef(position).getKey());
startActivity(foodlist);
}
});
}
"(Category.class, R.layout.menu_item, MenuViewHolder.class, category)" This line is in red and it states that FirebaseRecyclerAdapter() in FirebaseRecyclerAdapter cannot be applied to (a bunch of code here).
The FirebaseRecyclerAdapter binds a Query to a RecyclerView. When data is added, removed, or changed these updates are automatically applied to your UI in real time.
First, configure the adapter by building FirebaseRecyclerOptions:
FirebaseRecyclerOptions<Category> options =
new FirebaseRecyclerOptions.Builder<Category>()
.setQuery(category, Category.class)
.build();
Next create the FirebaseRecyclerAdapter object. You should already have a ViewHolder subclass for displaying each item.
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(options) {
#Override
public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.menu_item for each item
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.menu_item, parent, false);
return new MenuViewHolder(view);
}
#Override
protected void onBindViewHolder(MenuViewHolder holder, int position, Category model) {
// Bind the Chat object to the ChatHolder
// ...
}
};
You can find more information here:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#firebaseui-for-realtime-database
Related
I have a Imageview its load image from Firebase Database url with Picasso Image loader. I wish to hide imageview when firebase database url is empty. How to set Imageview setVisibility gone in that time.
My code is
LoadData(categoryId);
}
private void LoadData(String categoryId) {
options = new FirebaseRecyclerOptions.Builder<TrollModel>().setQuery(MCC,TrollModel.class).build();
adapter = new FirebaseRecyclerAdapter<TrollModel, TrollViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull TrollViewHolder trollViewHolder, int i, #NonNull TrollModel trollModel) {
trollViewHolder.thrillername.setText(trollModel.getTitle());
Picasso.get().load(trollModel.getImage())
.into(trollViewHolder.thrillersimage);
trollViewHolder.setItemClickListner(new ItemClickListner() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent detailsIntent = new Intent(TrollActivity.this, TrollDetailActivity.class);
detailsIntent.putExtra("CategoryId", adapter.getRef(position).getKey());
startActivity(detailsIntent);
}
});
}
#NonNull
#Override
public TrollViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.troll_items, parent, false);
return new TrollViewHolder(v);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
}
There is setVisibility() funciton for every View in android. You can find this if you google it in right way.
Refer below code to solve your problem.
if (trollModel.getImage().isEmpty()) {
trollViewHolder.thrillersimage.setVisibility(View.GONE);
} else {
trollViewHolder.thrillersimage.setVisibility(View.VISIBLE);
Picasso.get().load(trollModel.getImage())
.into(trollViewHolder.thrillersimage);
}
I have the FirebaseRecyclerAdapter declaration below:
private void updateList() {
adapter = new FirebaseRecyclerAdapter<User, ListOnlineViewHolder>(
User.class,
R.layout.user_layout,
ListOnlineViewHolder.class,
counterRef
) {
#Override
protected void onBindViewHolder(#NonNull ListOnlineViewHolder holder, int position, #NonNull User model) {
holder.txtEmail.setText((model.getEmail()));
}
#NonNull
#Override
public ListOnlineViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
};
adapter.notifyDataSetChanged();
listOnline.setAdapter(adapter);
}
The declaration of adapter = new FirebaseRecyclerAdapter<User, ListOnlineViewHolder> gives an error of FirebaseRecyclerAdapter cannot be applied. I am aware that I need to build FirebaseRecyclerOptions. How do I build 2 options?
First, configure the adapter by building FirebaseRecyclerOptions.For example:
FirebaseRecyclerOptions<User> options =
new FirebaseRecyclerOptions.Builder<User>()
.setQuery(query, User.class)
.build();
Next create the FirebaseRecyclerAdapter object. You should already have a ViewHolder subclass for displaying each item. For example:
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<User, ListOnlineViewHolder>(options) {
#Override
public ListOnlineViewHolder 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.message, parent, false);
return new ListOnlineViewHolder(view);
}
#Override
protected void onBindViewHolder(ListOnlineViewHolder holder, int position, User model) {
}
};
Check here for more info:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md
I have to develop a Recyclerview with PopulateViewHolder in an older version of Firebase. Now I'm trying to upgrade the version (not at the last one) and I don't know how to use onBindHolder or anything about this new kind of RecyclerView.
Here is how I had the setup:
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, EventViewHolder>(
Event.class,
R.layout.event_row,
EventViewHolder.class,
mDatabase.orderByChild("timeStamp")
) {
#Override
protected void populateViewHolder(final EventViewHolder viewHolder, final Event model, final int position) {
final String id = getRef(position).getKey().toString().trim();
viewHolder.setTitle(model.getTitle());
viewHolder.setDateWords(model.getDateWords());
viewHolder.setDesc(model.getDesc());
}
};
mEventList.setAdapter(firebaseRecyclerAdapter);
}
public static class EventViewHolder extends RecyclerView.ViewHolder {
View mView;
public EventViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
}
And here it's how I'm trying to do now:
Query searchQuery = mDatabase.orderByChild("timeStamp");
options = new FirebaseRecyclerOptions.Builder<Event>()
.setQuery(searchQuery, Event.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, EventViewHolder>(options) {
#Override
protected void onBindViewHolder(final EventViewHolder viewHolder, int position, Event model) {
final String id = getRef(position).getKey().toString().trim();
viewHolder.setTitle(model.getTitle());
viewHolder.setDateWords(model.getDateWords());
viewHolder.setDesc(model.getDesc());
}
#NonNull
#Override
public EventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
};
firebaseRecyclerAdapter.startListening();
mEventList.setAdapter(firebaseRecyclerAdapter);
The ViewHolder Class is the same.
Here is the error
Actually, I'm asking for tutorial, because I only find those that make the adapter separately, but I wanna have it in the same activity.
In your public EventViewHolder onCreateViewHolder method, instead of returning a view you are returning null. To solve this, please add the following lines of code inside this method:
#NonNull
#Override
public EventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new EventViewHolder(view);
}
In which item_layout is a custom layout for your item. If you want to use instead of this layout a built-in XML layout document that is part of the Android OS, you can use:
android.R.layout.simple_list_item_1
I am having trouble with populating my Firebase recycler adapter. What I want to do in the end is that a user searches for a name and the result from the database will populate the recyclerView.
The database structure looks like this:
The code to populate looks like this, this is all in onCreate():
Query mQuery = FirebaseDatabase.getInstance().getReference().orderByChild("users");
FirebaseRecyclerOptions<Users> options =
new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(mQuery, Users.class)
.build();
mAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.user_single_layout, parent, false);
return new UsersViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull Users model) {
holder.setName(model.getFirstName());
}
};
mUserList.setAdapter(mAdapter);
Right now I just want it to populate with all the users in the database but nothing seems to be populated.
mUserList is a recyclerView.
This is the UsersViewHolder class:
public static class UsersViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView mUsername;
UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
mUsername = mView.findViewById(R.id.user_single_name);
}
public void setName(String name) {
mUsername.setText(name);
}
}
To solve this, change the following line of code:
Query mQuery = FirebaseDatabase.getInstance().getReference().orderByChild("users");
with:
Query mQuery = FirebaseDatabase.getInstance().getReference()
.child("users")
.orderByChild("first_name");
You are missing a child. Don't also forget to start listening for changes:
#Override
protected void onStart() {
super.onStart();
mAdapter.startListening();
}
And also to stop listening when not needed.
#Override
protected void onStop() {
super.onStop();
if( mAdapter!= null) {
mAdapter.stopListening();
}
}
Use that way. Follow my code for yourself.
FirebaseRecyclerOptions<Data> options=
new FirebaseRecyclerOptions.Builder<Data>()
.setQuery(mIncomeDatabase,Data.class)
.setLifecycleOwner(this)
.build();
FirebaseRecyclerAdapter<Data, MyViewHolder> firebaseRecyclelerAdapter = new FirebaseRecyclerAdapter<Data, MyViewHolder>(options) {
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.income_recycler_data,parent,false));
}
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull Data model) {
holder.setName(model.getName());
holder.setAmount(model.getAmount());
holder.setType(model.getType());
holder.setNote(model.getNote());
holder.setAmount(model.getAmount());
holder.setDate(model.getDate());
}
};
recyclerView.setAdapter(firebaseRecyclelerAdapter);
Given below is my Firebase Schema:
Book {
Book-1: {title, author}
Book-2: {title, author}
Book-3: {title, author}
Book-4: {title, author}
}
I have listed them in RecyclerView and populating through FirebaseRecyclerAdapter. The code for the same is given below:
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Book");
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Book, BookListHolder>(
Book.class,
R.layout.simple_book_vertical_list,
BookListHolder.class,
ref) {
#Override
protected void populateViewHolder(BookListHolder viewHolder, Book model, int position) {
viewHolder.setBookTitle(model.getTitle());
viewHolder.setBookAuthor(model.getAuthor());
}
};
Now, after clicking the view, I want to show the data from the view that is clicked, i.e. their title and author.
What do I have to do?
Set on click listener:
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String key = firebaseRecyclerAdapter.getRef(mRecycler.getChildLayoutPosition(v)).getKey();
// do your magic here
}
});
return viewHolder;
}