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()));
Related
I try this one but can't see any post on search.
Private void firebaseSearch(String PostSearch){
String query = PostSearch.toLowerCase();
Query postsQuery =mDatabase.child(POSTS).limitToFirst(POSTS_QUERY_LIMIT).startAt(query).endAt(query + "\uf8ff");
mDatabase.keepSynced(true);
setUpRecyclerView();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
hideProgressDialog();
if( !isPopulateViewHolderCalled) showPostsList(true);
}
}, HANDLER_DELAY);
FirebaseRecyclerOptions<Post> options =
new FirebaseRecyclerOptions.Builder<Post>()
.setQuery(postsQuery, Post.class)
.build();
mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(options) {
#NonNull
#Override
public PostViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_post, parent, false);
return new PostViewHolder(view);
}
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);
}
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();
}
I found two similar questions but none of them worked. I am using fragment and RecyclerViews. The console displays the error
E/RecyclerView: No adapter attached; skipping layout
https://i.stack.imgur.com/dxk8y.png Adapter
https://i.stack.imgur.com/0J2io.png Fragment
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listkos, null);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
User user = entries.get(position);
holder.textViewTitle.setText(entries.get(position).getnmkos());
holder.textViewShortDesc.setText(entries.get(position).getHp());
}
#Override
public int getItemCount() {
return entries.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewTitle, textViewShortDesc;
ImageView imageView;
public MyViewHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.tvnm);
textViewShortDesc = itemView.findViewById(R.id.tvhp);
}
}
}
this fragment
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_beranda, container, false);
database = FirebaseDatabase.getInstance().getReference("Users");
firebaseAuth = FirebaseAuth.getInstance();
firebaseUser = firebaseAuth.getCurrentUser();
re = (RecyclerView) view.findViewById(R.id.recyle_buka);
rvLayoutManager = new LinearLayoutManager(getActivity());
re.setLayoutManager(rvLayoutManager);
loadEntries();
return view;
}
private void loadEntries(){
database.child("Users").child(firebaseUser.getUid()).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
entries= new ArrayList<>();
for (DataSnapshot noteDataSnapshot:dataSnapshot.getChildren()){
User user=noteDataSnapshot.getValue(User.class);
user.setKey(noteDataSnapshot.getKey());
entries.add(user);
}
rvAdapter = new RvAdapter(entries) ;// This is still empty, probably
re.setAdapter(rvAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.w("LogFragment", "loadLog:onCancelled", databaseError.toException());
}
});
}
}
still not working guys
/*It looks like your post is mostly code; please add some more detail */
You must be create a Linear Layout manager. You must be set adapter to the recyclerview.
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
re.setLayoutManager(linearLayoutManager);
YourAdapter yourAdapter = new YourAdapter(list);
re.setAdapter(yourAdapter);
E/RecyclerView: No adapter attached; skipping layout
It means you haven't attached the adapter to your RecyclerView. So, you need to initialized your RecyclerView with an empty adapter. Something like this:
re = (RecyclerView) view.findViewById(R.id.recyle_buka);
rvLayoutManager = new LinearLayoutManager(getActivity());
re.setLayoutManager(rvLayoutManager);
// initialize empty adapter
entries= new ArrayList<>();
rvAdapter = new RvAdapter(entries);
re.setAdapter(rvAdapter);
Then, whenever you need to reload and refresh the RecyclerView, you should not doing this:
private void loadEntries(){
...
entries= new ArrayList<>();
for (DataSnapshot noteDataSnapshot:dataSnapshot.getChildren()){
User user=noteDataSnapshot.getValue(User.class);
user.setKey(noteDataSnapshot.getKey());
entries.add(user);
}
// DON'T DO THIS!
rvAdapter = new RvAdapter(entries) ;
re.setAdapter(rvAdapter);
}
because it resetting your adapter with a new Adapter.
What you need is refreshing the list and notifying the adapter for data changes.
First, add the following method to your Adapter to reload the list:
public void swap(List<User> list) {
entries.clear();
entries.addAll(list);
notifyDataSetChanged();
}
Then add it to your loadEntries(). Something like this:
private void loadEntries() {
...
List<User> newEntries = new ArrayList<>();
for (DataSnapshot noteDataSnapshot:dataSnapshot.getChildren()){
User user=noteDataSnapshot.getValue(User.class);
user.setKey(noteDataSnapshot.getKey());
newEntries.add(user);
}
rvAdapter.swap(newEntries);
}
This happen because you didn't set layout manager to Recyclerview.
Refer below example.
Initialize recyclerview
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
Initialize your adapter class
mAdapter = new MoviesAdapter(movieList);
now set layout manager to recyleview
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
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);