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);
Related
I want firestore recyclerview adapter to load data only from the server not from cache. "I want the adapter display nothing if the device is offline." But I don't know the way to do this and I searched for it. What I found was the way to do this in the normal get(); method only to retrieve specific data, but I didn't find anyone to explain the way to do this with firebase firestore adapter.
that's my MainActivity code.
public class MainActivity extends AppCompatActivity {
private FirebaseFirestore db;
RecyclerView recyclerView;
private FirestoreRecyclerviewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
db=FirebaseFirestore.getInstance();
//Query
Query query = db.collection("collection");
FirestoreRecyclerOptions<ItemClass> options=new FirestoreRecyclerOptions.Builder<ItemClass>()
.setQuery(query, ItemClass.class)
.build();
recyclerView=findViewById(R.id.recycler);
final LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
adapter = new FirestoreRecyclerviewAdapter( options, this );
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
and this is adapter class...
Firestore Adapter
public class FirestoreRecyclerviewAdapter extends FirestoreRecyclerAdapter<ItemClass, FirestoreRecyclerviewAdapter.MyViewHolder> {
public FirestoreRecyclerviewAdapter (#NonNull FirestoreRecyclerOptions<ItemClass> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull ItemClass item) {
//Binding code.
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
}
public class MyViewHolder extends RecyclerView.ViewHolder {
//view components
public MyViewHolder(#NonNull View itemView) {
super(itemView);
//initializing item components
}
}
}
The simplest approach I can think of is disabling disk persistence, which you can do with:
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(false)
.build();
db.setFirestoreSettings(settings);
Don't load/fetch data when the device has no internet connection, notify user that his/her device is offline and if want the service should make sure the device has an internet connection.
As mentioned in the title, my recyclerview does not work. No idea why, because I do not get any errors or anything in the logcat or while building the app. Is there a way I could debug this or could you point out the mistake that I did?
I've implemented another recyclerview with the adapter, but I used a simple adapter. I tried using the firebaseUI in this case, and it seems that the adapter somehow cannot retrieve the data from the firebase firestore.
https://imgur.com/a/JSLXuou The screenshot of my database structure.
My adapter:
public class ManageAdapter extends FirestoreRecyclerAdapter<Manage, ManageAdapter.ManageHolder> {
public ManageAdapter(#NonNull FirestoreRecyclerOptions<Manage> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ManageHolder holder, int position, #NonNull Manage model) {
holder.textviewName.setText(model.getName());
holder.textviewGrade.setText(model.getGrade());
//holder.textviewAge.setText(String.valueOf(model.getAge()));
}
#NonNull
#Override
public ManageHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.manage_item,
parent,false);
return new ManageHolder(v);
}
class ManageHolder extends RecyclerView.ViewHolder{
TextView textviewName;
TextView textviewAge;
TextView textviewGrade;
public ManageHolder(#NonNull View itemView) {
super(itemView);
textviewAge = itemView.findViewById(R.id.manage_item_title_age);
textviewGrade = itemView.findViewById(R.id.manage_item_grade);
textviewName = itemView.findViewById(R.id.manage_item_name);
}
}
}
Main class:
private FirebaseFirestore firebaseFirestore;
private Toolbar mToolbar;
private Button delete_event;
private RecyclerView participants;
private TextView manage_events_title;
private TextView participans_text;
private ManageAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_events);
final String ID = getIntent().getExtras().getString("ID");
firebaseFirestore = FirebaseFirestore.getInstance();
delete_event = (Button) findViewById(R.id.delete_event);
manage_events_title = (TextView) findViewById(R.id.manage_events_title);
participans_text = (TextView) findViewById(R.id.participants_text);
participants = (RecyclerView) findViewById(R.id.recyclerview_manage);
setUpRecyclerView();
}
private void setUpRecyclerView(){
final String ID = getIntent().getExtras().getString("ID");
Query query = firebaseFirestore.collection("Events").document(ID).collection("Users");
FirestoreRecyclerOptions<Manage> options = new FirestoreRecyclerOptions.Builder<Manage>()
.setQuery(query, Manage.class).build();
adapter = new ManageAdapter(options);
participants.setHasFixedSize(true);
participants.setLayoutManager(new LinearLayoutManager(this));
participants.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
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
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
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