recyclerviewadapter not working [duplicate] - android

I'm try to retrieve datafrom database in my fragment onViewCreated method I have used FirebaseRecylerAdapter like below
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter;
}
public static class AllUsersViewHolder extends RecyclerView.ViewHolder {
View view;
public AllUsersViewHolder(View itemView) {
super(itemView);
view = itemView;
}
}
when I use firebaseRecyclerAdapter = new FirebaseRecyclerAdapter it wont give me the populateViewHolder method just give method like below
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>() {
#Override
protected void onBindViewHolder(AllUsersViewHolder holder, int position, AllUsers model) {
}
#Override
public AllUsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
};
Can someone please tell me why this is happening

The constructor of the Firebase...Adapter classes has changed in FirebaseUI version 3. Given this query:
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("users")
.equalTo(name);
If you're using a version 3 or higher, you use FirebaseRecyclerOptions:
FirebaseRecyclerOptions<model_class_name> options =
new FirebaseRecyclerOptions.Builder<model_class_name>()
.setQuery(query, model_class_name.class)
.build();
Then declare a FirebaseRecyclerAdapter:
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
add the variable options as above. It is the variable of the class FirebaseRecyclerOptions
Then to add items you have to use onBindViewHolder since its the latest version of firebase ui:
#Override
protected void onBindViewHolder(Holder holder, int position, model_class_name model) {
// Bind the class object to the holder
// ...
}
For more info check this: https://github.com/firebase/FirebaseUI-Android/tree/master/database

Related

The error is inn the firebaseRecyclerAdapter view adapter section

the error message is : incompatible types: FirestoreRecyclerOptions cannot be converted to FirebaseRecyclerOptions ----
and the code is as follows.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agencies);
firebaseFirestore= FirebaseFirestore.getInstance();
//views definition
agencyFeed = (RecyclerView)findViewById(R.id.agencyRecyclerView);
//------Requirements for retrieving data and storing it to recycler view---
//Query ----- for retrieving data from firestore
Query query = firebaseFirestore.collection("Agencies");
// recyclerOptions
FirestoreRecyclerOptions<AgencyModel> options = new FirestoreRecyclerOptions.Builder<AgencyModel>()
.setQuery(query,AgencyModel.class)
.build();
adapter = new FirebaseRecyclerAdapter<AgencyModel, AgencyViewHolder>(options){
#NonNull
#Override
public AgencyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.agency_cards,parent,false);
return new AgencyViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull AgencyViewHolder holder, int position, #NonNull AgencyModel model) {
holder.name.setText(model.getName());
holder.location.setText(model.getLocation());
}
};
agencyFeed.hasFixedSize();
agencyFeed.setLayoutManager(new GridLayoutManager(this,2));
agencyFeed.setAdapter(adapter);
}
//viewholder class
public class AgencyViewHolder extends RecyclerView.ViewHolder {
private TextView name, location;
public AgencyViewHolder(#NonNull View itemView) {
super(itemView);
name= itemView.findViewById(R.id.agency_name); //name of agency
location= itemView.findViewById(R.id.locationTextView);
}
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
}
You are using Firestore according to your query, therefore you have to use FirestoreRecyclerAdapter and not FirebaseRecyclerAdapter. Therefore change:
adapter = new FirebaseRecyclerAdapter<AgencyModel, AgencyViewHolder>(options){
into this:
adapter = new FirestoreRecyclerAdapter<AgencyModel, AgencyViewHolder>(options){

Why my recycler View is not Showing anything

I am trying to show all my users. it's not showing even a single of them. I do not know why but my adapter is not working and I also even tried the adapter.startlistening() method. but do not know where to put it. could anyone please just help me?
public class AllUsersActivity extends AppCompatActivity {
private RecyclerView allUsersList;
private DatabaseReference databaseReference;
private FirebaseRecyclerOptions<Users> options;
private FirebaseRecyclerAdapter firebaseRecyclerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_users);
allUsersList = (RecyclerView)findViewById(R.id.users_list);
allUsersList.setHasFixedSize(true);
allUsersList.setLayoutManager(new LinearLayoutManager(this));
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
}
#Override
protected void onStart() {
super.onStart();
options = new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(databaseReference, Users.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_user_layout, parent,false);
return new UsersViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder usersViewHolder, int i, Users users) {
usersViewHolder.setName(users.getName());
}
};
allUsersList.setAdapter(firebaseRecyclerAdapter);
}
}
Assuming that this is the database:
Users
|
|---------UID1
---name
---image
---.....
----------UID2
---name
---image
---......
Make sure that your class Users has the fields exactly like in the database:
class Users{
//these fields must be typed exactly like in the database (watch out for upper case and lower case)
private String name;
private String image;
.........
.........
//also must have a constructor for all fields
//also must have getters and setters for all fields
}
Also in onStart() add this line:
firebaseRecyclerAdapter.startListening();
Directly under this line:
allUsersList.setAdapter(firebaseRecyclerAdapter);

Cannot apply FirebaseRecyclerAdapter? [duplicate]

I'm try to retrieve datafrom database in my fragment onViewCreated method I have used FirebaseRecylerAdapter like below
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter;
}
public static class AllUsersViewHolder extends RecyclerView.ViewHolder {
View view;
public AllUsersViewHolder(View itemView) {
super(itemView);
view = itemView;
}
}
when I use firebaseRecyclerAdapter = new FirebaseRecyclerAdapter it wont give me the populateViewHolder method just give method like below
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>() {
#Override
protected void onBindViewHolder(AllUsersViewHolder holder, int position, AllUsers model) {
}
#Override
public AllUsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
};
Can someone please tell me why this is happening
The constructor of the Firebase...Adapter classes has changed in FirebaseUI version 3. Given this query:
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("users")
.equalTo(name);
If you're using a version 3 or higher, you use FirebaseRecyclerOptions:
FirebaseRecyclerOptions<model_class_name> options =
new FirebaseRecyclerOptions.Builder<model_class_name>()
.setQuery(query, model_class_name.class)
.build();
Then declare a FirebaseRecyclerAdapter:
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
add the variable options as above. It is the variable of the class FirebaseRecyclerOptions
Then to add items you have to use onBindViewHolder since its the latest version of firebase ui:
#Override
protected void onBindViewHolder(Holder holder, int position, model_class_name model) {
// Bind the class object to the holder
// ...
}
For more info check this: https://github.com/firebase/FirebaseUI-Android/tree/master/database

How to populate recyclerview in firebase(populateview method is not working) [duplicate]

I'm try to retrieve datafrom database in my fragment onViewCreated method I have used FirebaseRecylerAdapter like below
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter;
}
public static class AllUsersViewHolder extends RecyclerView.ViewHolder {
View view;
public AllUsersViewHolder(View itemView) {
super(itemView);
view = itemView;
}
}
when I use firebaseRecyclerAdapter = new FirebaseRecyclerAdapter it wont give me the populateViewHolder method just give method like below
FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>() {
#Override
protected void onBindViewHolder(AllUsersViewHolder holder, int position, AllUsers model) {
}
#Override
public AllUsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
};
Can someone please tell me why this is happening
The constructor of the Firebase...Adapter classes has changed in FirebaseUI version 3. Given this query:
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("users")
.equalTo(name);
If you're using a version 3 or higher, you use FirebaseRecyclerOptions:
FirebaseRecyclerOptions<model_class_name> options =
new FirebaseRecyclerOptions.Builder<model_class_name>()
.setQuery(query, model_class_name.class)
.build();
Then declare a FirebaseRecyclerAdapter:
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
add the variable options as above. It is the variable of the class FirebaseRecyclerOptions
Then to add items you have to use onBindViewHolder since its the latest version of firebase ui:
#Override
protected void onBindViewHolder(Holder holder, int position, model_class_name model) {
// Bind the class object to the holder
// ...
}
For more info check this: https://github.com/firebase/FirebaseUI-Android/tree/master/database

Firebase FirebaseRecyclerAdapter not working

I'm trying to populate recyclerView with firebase contents but i'm catching error.
This my activity fragment
public class Journal extends Fragment {
RecyclerView recyclerView;
DatabaseReference myref;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_journal,container,false);
recyclerView=(RecyclerView)v.findViewById(R.id.journal_rv);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
myref= FirebaseDatabase.getInstance().getReference().child("/blog");
// here is the error : "Must implements methods"
FirebaseRecyclerAdapter<JournalEntry,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<JournalEntry, BlogViewHolder>(
JournalEntry.class,
R.layout.journal_list_item,
BlogViewHolder.class,
myref
) {
// Another one : method does not override from its superclass
#Override
protected void populateViewHolder(BlogViewHolder viewHolder, JournalEntry model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDescription(model.getDescription());
}
};
recyclerView.setAdapter(recyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView textView_title;
TextView textView_decription;
public BlogViewHolder(View itemView) {
super(itemView);
mView=itemView;
textView_title = (TextView)itemView.findViewById(R.id.journal_title);
textView_decription = (TextView) itemView.findViewById(R.id.journal_event_title);
}
public void setTitle(String title)
{
textView_title.setText(title+"");
}
public void setDescription(String description)
{
textView_decription.setText(description);
}
When i implement those methods, I always catch the same error;
can anyone help me?
Ps: I'm using a fragment not an activity.
UPDATE i implemented methods like this
FirebaseRecyclerAdapter<JournalEntry,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<JournalEntry, BlogViewHolder>(
JournalEntry.class,
R.layout.journal_list_item,
BlogViewHolder.class,
myref
) {
#Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
#Override
protected void onBindViewHolder(BlogViewHolder holder, int position, JournalEntry model) {
holder.setTitle(model.getEvent_title());
holder.setDescription(model.getEvent_content());
}
};
now the error (inside editor) is FirebaseAdaper in firebaseAdapter can not be applied to :
JournalEntry.class  (java...consti.last.database.JournalEntry>)
R.layout.journal_list_item  (int)
BlogViewHolder.class  (java...consti.last.Journal.BlogViewHolder>)
myref  (com...firebase.database.DatabaseReference)
recyclerView.setHasFixedSize(true);
Remove this line from your code
To solve this you need to add the following line of code at the end of your onCreateView() method:
return v;
If have also have other unimplemented methods, just press in your Android Studio ALT+Insert -> select Override Methods -> select the nedded methods.
I had a similar problem and removed the
recyclerView.setHasFixedSize(true); line and it worked. I just don't understand why. Please enlighten me

Categories

Resources