My recyclerview doesn't show up when I change my activity - android

When my app start up the recyclerview loads up. But when I change my activity, like, when i go to ProfileActivity from DashboardActivity and Come back to DashboardActivity the recyclerview vanishes. I don't get the problem.
This part is in onCreate method
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setAdapter(adapter);
adapter.startListening();
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
This is the onStart method
#Override
protected void onStart() {
super.onStart();
Query query = firebaseFirestore.collection("requests")
.orderBy("creationTime", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Request> options = new FirestoreRecyclerOptions.Builder<Request>()
.setQuery(query, Request.class)
.build();
FirestoreRecyclerAdapter<Request, RequestViewHolder> adapter =
new FirestoreRecyclerAdapter<Request, RequestViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull RequestViewHolder holder, int i, #NonNull Request request) {
holder.requestUserNameTV.setText(request.getUser().getFullName());
holder.requestCreationTimeTV.setText(request.getCreationTime());
holder.requestedBloodGroupTV.setText(request.getBloodGroup());
holder.requestLocationTV.setText(request.getLocation());
holder.requestMessageTV.setText(request.getMessage());
}
#NonNull
#Override
public RequestViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
return new RequestViewHolder(v);
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
This is the ViewHolder
public class RequestViewHolder extends RecyclerView.ViewHolder {
public TextView requestUserNameTV, requestCreationTimeTV, requestedBloodGroupTV;
public TextView requestLocationTV, requestMessageTV;
public LinearLayout ll;
public RequestViewHolder(#NonNull View itemView) {
super(itemView);
ll = itemView.findViewById(R.id.ll);
requestUserNameTV = itemView.findViewById(R.id.requestUserNameTV);
requestCreationTimeTV = itemView.findViewById(R.id.requestCreationTimeTV);
requestedBloodGroupTV = itemView.findViewById(R.id.requestedBloodGroupTV);
requestLocationTV = itemView.findViewById(R.id.requestedLocationTV);
requestMessageTV = itemView.findViewById(R.id.requestMessageTV);
}
}

Move your code from OnStart to OnCreate. it should solve your problem

Now I'm doing it this way. and it works.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
setUpRecyclerView();
}
private void setUpRecyclerView() {
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
firebaseFirestore = FirebaseFirestore.getInstance();
Query query = firebaseFirestore.collection("requests")
.orderBy("creationTime", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Request> options = new FirestoreRecyclerOptions.Builder<Request>()
.setQuery(query, Request.class)
.build();
FirestoreRecyclerAdapter<Request, RequestViewHolder> adapter =
new FirestoreRecyclerAdapter<Request, RequestViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull RequestViewHolder holder, int i, #NonNull Request request) {
holder.requestUserNameTV.setText(request.getUser().getFullName());
holder.requestCreationTimeTV.setText(request.getCreationTime());
holder.requestedBloodGroupTV.setText(request.getBloodGroup());
holder.requestLocationTV.setText(request.getLocation());
holder.requestMessageTV.setText(request.getMessage());
}
#NonNull
#Override
public RequestViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
RequestViewHolder requestViewHolder = new RequestViewHolder(v);
return requestViewHolder;
}
};
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
adapter.startListening();
}

Related

FirebaseRecyclerOptions with spinner onItemSelected query issue

I have a spinner with locations and I would like to click on the location from the spinner and filter my Firebase data to show all items with that location selected.
I have tried putting the "onItemSelected" code fragment in the onStart area before and after the FirebaseRecyclerOptions and when I open the app and select the location from the spinner nothing new populates. It still shows all the data and all the locations.
public class MainActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
Spinner mLocationSpinner;
DatabaseReference mRef;
FirebaseDatabase mFirebaseDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recyclerview);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = (DatabaseReference) mFirebaseDatabase.getReference("Data");
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Model> options =
new FirebaseRecyclerOptions.Builder<Model>()
.setQuery(mRef, Model.class)
.build();
FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder , int position , #NonNull Model model) {
holder.setDetails(getApplicationContext(), model.getBusiness(), model.getImage());
mLocationSpinner = (SearchableSpinner) findViewById(R.id.locationSpinner);
ArrayAdapter<CharSequence> spinAdapter = ArrayAdapter.createFromResource(MainActivity.this,
R.array.location_arrays, android.R.layout.simple_spinner_item);
spinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mLocationSpinner.setAdapter(spinAdapter);
mLocationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent , View view , int position , long id) {
String locationSpinner = parent.getItemAtPosition(position).toString();
Query querySpinner = mRef.orderByChild("location").equalTo(String.valueOf(mLocationSpinner));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent , int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row, parent, false);
return new ViewHolder(view);
}
};
firebaseRecyclerAdapter.startListening();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}

Attempting Horizontal Firebase RecyclerView with MultiViews

Im attempting a FireBase Horizontal RecyclerView (like an Netflix clone).
I have looked at Git and Android Arsenal to find programing similar, but had been unsuccessful finding anything with a firebase backend. I had played with the adapter and the database. I have work on this for a couple days without finding a solution.
public class RestaurantList extends AppCompatActivity {
RecyclerView recyclerView1;
RecyclerView recyclerView2;
FirebaseRecyclerOptions<Restaurant> options = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference()
.child("Restaurants1")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter = new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
FirebaseRecyclerOptions<Restaurant> options2 = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference()
.child("Restaurants2")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter2= new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options2) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter2.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_list);
RelativeLayout relativeLayout = findViewById(R.id.root2_layout);
AnimationDrawable animationDrawable = (AnimationDrawable)relativeLayout.getBackground();
animationDrawable.setEnterFadeDuration(5);
animationDrawable.setExitFadeDuration(1000);
animationDrawable.start();
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
LinearLayoutManager layoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
recyclerView1 = (RecyclerView) findViewById(R.id.recycler_restaurant);
recyclerView1.setLayoutManager(layoutManager);
recyclerView2 = (RecyclerView) findViewById(R.id.recycler_restaurant2);
recyclerView1.setLayoutManager(layoutManager2);
loadAsianFood();
loadSteakHouse();
}
private void loadSteakHouse() {
adapter.startListening();
recyclerView1.setAdapter(adapter);
recyclerView1.getAdapter().notifyDataSetChanged();
recyclerView1.scheduleLayoutAnimation();
}
private void loadAsianFood() {
adapter2.startListening(); // Did you mean adapter2.startListening()?
recyclerView2.setAdapter(adapter2);
recyclerView2.getAdapter().notifyDataSetChanged();
recyclerView2.scheduleLayoutAnimation();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
adapter2.stopListening();;
}
#Override
protected void onResume() {
super.onResume();
if (adapter != null)
adapter.startListening();
adapter2.startListening();
}
}
It only runs the first category (Restaurant1) only.
A few problems I noticed, though I don't know if they'll solve your actual issue.
1 - You are calling startListening on the first adapter when it should be the second:
private void loadAsianFood() {
adapter.startListening(); // Did you mean adapter2.startListening()?
recyclerView2.setAdapter(adapter2);
recyclerView2.getAdapter().notifyDataSetChanged();
recyclerView2.scheduleLayoutAnimation();
}
2 - You are only stopping the first adapter, not the second.
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
3 - You are only resuming the first adapter, not the second:
#Override
protected void onResume() {
super.onResume();
if (adapter != null)
adapter.startListening();
}
RecyclerView recyclerView1;
RecyclerView recyclerView2;
FirebaseRecyclerOptions<Restaurant> options = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference("RestaurantCategory")
.child("01").child("SteakHouse")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter = new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
FirebaseRecyclerOptions<Restaurant> options2 = new FirebaseRecyclerOptions.Builder<Restaurant>()
.setQuery(FirebaseDatabase.getInstance()
.getReference("RestaurantCategory")
.child("02").child("AsianFood")
,Restaurant.class)
.build();
FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter2= new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options2) {
#NonNull
#Override
public RestaurantViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.restaurant_item, parent, false);
return new RestaurantViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull RestaurantViewHolder viewHolder, int position, #NonNull Restaurant model) {
viewHolder.txt_restaurant_name.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.img_restaurant);
final Restaurant clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList = new Intent(RestaurantList.this, Home.class);
Common.restaurantSelected=adapter2.getRef(position).getKey();
startActivity(foodList);
}
});
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_list);
RelativeLayout relativeLayout = findViewById(R.id.root2_layout);
AnimationDrawable animationDrawable = (AnimationDrawable)relativeLayout.getBackground();
animationDrawable.setEnterFadeDuration(5);
animationDrawable.setExitFadeDuration(1000);
animationDrawable.start();
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
LinearLayoutManager layoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
recyclerView1 = (RecyclerView) findViewById(R.id.recycler_restaurant);
recyclerView1.setLayoutManager(layoutManager);
recyclerView2 = (RecyclerView) findViewById(R.id.recycler_restaurant2);
recyclerView1.setLayoutManager(layoutManager2);
loadAsianFood();
loadSteakHouse();
}
private void loadSteakHouse() {
adapter.startListening();
recyclerView1.setAdapter(adapter);
recyclerView1.getAdapter().notifyDataSetChanged();
recyclerView1.scheduleLayoutAnimation();
}
private void loadAsianFood() {
adapter2.startListening(); // Did you mean adapter2.startListening()?
recyclerView2.setAdapter(adapter2);
recyclerView2.getAdapter().notifyDataSetChanged();
recyclerView2.scheduleLayoutAnimation();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
adapter2.stopListening();;
}
#Override
protected void onResume() {
super.onResume();
if (adapter != null)
adapter.startListening();
adapter2.startListening();
}
}

setAdapter in RecyclerView cannot be applied to FirebaseRecyclerAdapter

i am trying to fetch data from firebase Realtime database and showing the data in recycler view but it shows an error
recyclerView = findViewById(R.id.RecyclerView);
recyclerView.setHasFixedSize(true);
databaseReference = firebaseDatabase.getInstance().getReference().child("Collection");
options = new FirebaseRecyclerOptions.Builder<CategoryItem>().setQuery(databaseReference,CategoryItem.class).build();
adapter = new FirebaseRecyclerAdapter<CategoryItem, CtegoryViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull CtegoryViewHolder holder, int position, #NonNull CategoryItem model) {
Picasso.get().load(model.getImageLink()).into(holder.i1, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
holder.t1.setText(model.getName());
}
#NonNull
#Override
public CtegoryViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view_layout,viewGroup,false);
return new CtegoryViewHolder(view);
}
};
androidx.recyclerview.widget.GridLayoutManager gridLayoutManager = new GridLayoutManager(this,1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter.startListening();
recyclerView.setAdapter(adapter);

Retrieve data from onbindviewholder using FirebaseRecyclerAdapter android

I'm working with firebase in android. The problem I facing is data stored on my database but I can't show them. Here is my code :
final FirebaseRecyclerOptions<Blog> options =
new FirebaseRecyclerOptions.Builder<Blog>()
.setQuery(databaseReference, Blog.class)
.build();
final FirebaseRecyclerAdapter<Blog, BlogViewHolder>
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);
return new BlogViewHolder(view);
}
#Override
protected void onBindViewHolder(BlogViewHolder holder, int position, Blog model) {
holder.setDesc(model.getDescription());
holder.setTitle(model.getTitle());
Log.w("ok","ok");
}
How can i show them in my view.
You may try this...
recyclerView = (RecyclerView)findViewById(R.id.mRecyclerView);
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(llm);

Firestore Recyclerview: No content is being loaded

I've tried the following code to try firestore recyclreview. But it looks like no content is being loaded. And yes, adapter.startListening() is being called onStart.
query = FirebaseFirestore.getInstance()
.collection("users")
.document(mAuth.getCurrentUser().getUid())
.collection("posts");
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
FirestoreRecyclerOptions<Post> options = new FirestoreRecyclerOptions.Builder<Post>()
.setQuery(query, Post.class)
.build();
adapter = new FirestoreRecyclerAdapter<Post, NewsfeedViewholder>(options) {
#Override
protected void onBindViewHolder(NewsfeedViewholder holder, int position, Post model) {
holder.setView(model.getContent());
}
#Override
public NewsfeedViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_post, parent, false);
return new NewsfeedViewholder(view);
}
};
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

Categories

Resources