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);
Related
This is the code in which I'm getting errors in startListening() and stopListening():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_app_cart,container,false);
recyclerView = view.findViewById(R.id.recylervieww);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
cartmodelList = new ArrayList<>();
cartadapt = new cartAdapter(getActivity(),cartmodelList);
recyclerView.setAdapter(cartadapt);
FirebaseRecyclerOptions<Cartmodel> options =
new FirebaseRecyclerOptions.Builder<Cartmodel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Cart List"), Cartmodel.class)
.build();
cartadapt = new cartAdapter(options);
recyclerView.setAdapter(cartadapt);
return view;
}
#Override
public void onStart() {
super.onStart();
cartadapt.startListening();
}
#Override
public void onStop() {
super.onStop();
cartadapt.stopListening();
}
}
This is the code for cartAdapter:
public class cartAdapter extends RecyclerView.Adapter<cartAdapter.ViewHolder> {
Context context;
List<Cartmodel> cartmodelList;
public cartAdapter(Context context, List<Cartmodel> cartmodelList) {
this.context = context;
this.cartmodelList = cartmodelList;
}
public cartAdapter(FirebaseRecyclerOptions<Cartmodel> options) {
}
#NonNull
#Override
public cartAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_singlero,parent,false));
}
#Override
public void onBindViewHolder(#NonNull cartAdapter.ViewHolder holder, int position) {
holder.productName.setText(cartmodelList.get(position).getProductName());
holder.quantity.setText(cartmodelList.get(position).getQuantity());
holder.totalPrice.setText(cartmodelList.get(position).getTotalPrice());
Glide.with(holder.itemView.getContext()).load(Cartmodel.productImage()).into(holder.productImage);
}
#Override
public int getItemCount() {
return cartmodelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView date,productName,productPrice,quantity,totalPrice;
ImageView productImage;
public ViewHolder(#NonNull View itemView) {
super(itemView);
productName = itemView.findViewById(R.id.pnametext);
quantity = itemView.findViewById(R.id.quantity);
totalPrice = itemView.findViewById(R.id.totalprice);
productImage = itemView.findViewById(R.id.img2);
}
}
}
I'm getting these errors:
Cannot resolve method 'startListening' in 'cartAdapter'
Cannot resolve method 'stopListening' in 'cartAdapter'
I'm using both classes in another activity but in those, I'm not getting any errors.
I'm getting error in startListening() and stopListening()
Because those methods are not present inside RecyclerView.Adapter class. Your adapter class should extend FirebaseRecyclerAdapter class instead in order to be able to call startListening() and stopListening().
For more info, please check the official documentation:
https://firebaseopensource.com/projects/firebase/firebaseui-android/database/readme/
I'm using two firebase UI Recycler views in a single activity. The first recycler view is working fine but I don't know why the second Recycler view is not populating. One thing I noticed that in the second recycler view 'OnBind' and 'OnCreate' methods are not called up?
Basically, I'm trying to use two horizontal recycler views within a single activity.
public class MainActivity extends AppCompatActivity {
private FirebaseRecyclerOptions<Video> options;
private FirebaseRecyclerAdapter<Video, MyViewHolder> adapter;
private RecyclerView recyclerViewVideo;
private FirebaseRecyclerOptions<Meme> optionsForHindiJokes;
private FirebaseRecyclerAdapter<Meme, MyViewHolder> adapterForHindiJokes;
private RecyclerView recyclerViewForHindiJokes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerViewVideo = findViewById(R.id.recycler_view_video);
recyclerViewVideo.setHasFixedSize(true);
recyclerViewVideo.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
recyclerViewForHindiJokes = findViewById(R.id.recycler_view_hindi_jokes);
recyclerViewForHindiJokes.setHasFixedSize(true);
recyclerViewForHindiJokes.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
/* **/
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference refToVideo1 = database.getReferenceFromUrl("https://memlo-a9291-default-rtdb.firebaseio.com/AppStructure/Videos/Video");
refToVideo1.keepSynced(true);
Query refToVideo = database.getReferenceFromUrl("https://memlo-a9291-default-rtdb.firebaseio.com/AppStructure/Videos/Video").limitToLast(20);
options = new FirebaseRecyclerOptions.Builder<Video>().setQuery(refToVideo, Video.class).build();
adapter= new FirebaseRecyclerAdapter<Video, MyViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull final Video model) {
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_memlo_without_background);
requestOptions.error(R.drawable.ic_memlo_without_background);
Glide.with(MainActivity.this)
.load(model.getVideoUrl())
.apply(requestOptions)
.thumbnail(Glide.with(MainActivity.this).load(model.getVideoUrl()))
// .centerCrop()
.into(holder.imageOnCard);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent1 = new Intent(MainActivity.this, VideoPlayer.class);
intent1.putExtra("VIDEO_URL", model.getVideoUrl());
startActivity(intent1);
}
});
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_cards_layout, parent, false);
return new MyViewHolder(v);
}
};
adapter.startListening();
recyclerViewVideo.setAdapter(adapter);
////////////////////////////////////////////////
Query refToHindiJokes = database.getReferenceFromUrl("https://memlo-a9291-default-rtdb.firebaseio.com/AppStructure/Memes/Category/Hindi%20Jokes");
optionsForHindiJokes = new FirebaseRecyclerOptions.Builder<Meme>().setQuery(refToHindiJokes, Meme.class).build();
adapterForHindiJokes = new FirebaseRecyclerAdapter<Meme, MyViewHolder>(optionsForHindiJokes) {
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull Meme model) {
Toast.makeText(getApplicationContext(),"Calling", Toast.LENGTH_LONG).show();
Glide.with(getApplicationContext())
.load(model.getImageUrl())
.into(holder.imageOnCard);
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v1=LayoutInflater.from(getApplicationContext()).inflate(R.layout.single_cards_layout, parent,false);
return new MyViewHolder(v1);
}
};
adapterForHindiJokes.startListening();
adapterForHindiJokes.notifyDataSetChanged();
recyclerViewForHindiJokes.setAdapter(adapterForHindiJokes);
}
}
This is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_user);
// getting id
UserList=findViewById(R.id.ChatUserRecycle);
toolbar=findViewById(R.id.mainPageBar);
databaseReference=FirebaseDatabase.getInstance().getReference().child("Student");
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Users"); //setting action bar title
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
UserList.setHasFixedSize(true);
UserList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<ChatUsers> options=
new FirebaseRecyclerOptions.Builder<ChatUsers>()
.setQuery(databaseReference,ChatUsers.class)
.setLifecycleOwner(this)
.build();
firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<ChatUsers,UsersViewHolder>(options) {
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new UsersViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.user_layout, parent, false));
}
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull ChatUsers model) {
holder.setName(model.getName());
}
};
UserList.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder{
View mview;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
mview=itemView;
}
public void setName(String name){
Toast.makeText(itemView.getContext(),"2",Toast.LENGTH_SHORT).show();
TextView name1=(TextView)mview.findViewById(R.id.SingleUsername);
name1.setText(name);
}
}
}
When I run my code nothing show just empty layout without recycleview and layout which want to I inflate. And after few seconds app.
FirebaseRecyclerAdapter<image_details, BlogViewHolder> adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(
image_details.class,
R.layout.individual_row,
myRef) {
#Override
protected void onBindViewHolder(BlogViewHolder holder, int position, image_details model) {
holder.setDate(model.getDate());
holder.setUrl(model.getUrl());
}
#Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
};
First I use this method but it also not works.
When I run my code nothing show just empty layout
This is because you're not creating a view for your holder at all. To solve this, please add the following lines of code:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.individual_row, parent, false);
return new BlogViewHolder(view);
Inside your onCreateViewHolder() method and remove return null;.
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
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<AllUsers, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, UsersViewHolder>() {
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull AllUsers model) {
holder.setName(model.getName());
holder.setStatus(model.getStatus());
}
};
mUsersList.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder{
View mView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name) {
TextView mDisplayName = (TextView) mView.findViewById(R.id.display_name);
mDisplayName.setText(name);
}
public void setStatus(String status) {
TextView mDisplayStatus = (TextView) mView.findViewById(R.id.display_status);
mDisplayStatus.setText(status);
}
}
The error is in the following line in which theres a red line showing under ():
FirebaseRecyclerAdapter<AllUsers, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, UsersViewHolder>() {
What should be the parameters that come inside that?
You need to do the following:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
Query query=ref.child("Users").orderByChild("name").equalTo(name);
FirebaseRecyclerOptions<AllUsers> options =
new FirebaseRecyclerOptions.Builder<AllUsers>()
.setQuery(query, AllUsers.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<AllUsers, UsersViewHolder>(options) {
First, you need to configure the adapter by building FirebaseRecyclerOptions
AllUsers is your POJO class, and you need to make a query to have in the recycler adapter.
more info here:
https://github.com/firebase/FirebaseUI-Android/tree/master/database