Hello! At the beginning I must tell that I've shown many documentations about Firebase and spent a lot of time on existing topics on Stack.
Let's begin. I'm creating app for car users with chat (Called Response) including Firebase implementation. Now I'm struggling with showing list of users using recyclerView. In firebase I have database "Users". After using my code I have blank activity without anyone user.
In 'com.firebaseui:firebase-ui-database:2.4.0' (with populateViewHolder) my acitivity works fine. Is anybody know what's wrong with my code? Thanks for any reply :)
This is my Activity:
public class ResponseAllUsersActivity extends AppCompatActivity {
private Toolbar mToolbar;
private RecyclerView allUsersList;
private DatabaseReference allDatabaseUserreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_response_all_users);
mToolbar = (Toolbar) findViewById(R.id.all_users_app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("All Users");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
allUsersList = (RecyclerView) findViewById(R.id.all_users_list);
allUsersList.setHasFixedSize(true);
allUsersList.setLayoutManager(new LinearLayoutManager(this));
allDatabaseUserreference = FirebaseDatabase.getInstance().getReference().child("Users");
}
@Override
protected void onStart() {
super.onStart();
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Users");
FirebaseRecyclerOptions<Response_AllUsers> options =
new FirebaseRecyclerOptions.Builder<Response_AllUsers>()
.setQuery(query, Response_AllUsers.class)
.build();
FirebaseRecyclerAdapter<Response_AllUsers, AllUsersViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Response_AllUsers, AllUsersViewHolder>
(options)
{
@Override
protected void onBindViewHolder(@NonNull AllUsersViewHolder holder, int position, @NonNull Response_AllUsers model)
{
holder.setUser_name(model.getUser_name());
holder.setUser_status(model.getUser_status());
holder.setUser_image(getApplicationContext(), model.getUser_image());
}
@NonNull
@Override
public AllUsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.response_users_display_layout,parent,false);
return new AllUsersViewHolder(view);
}
};
allUsersList.setAdapter(firebaseRecyclerAdapter);
}
public static class AllUsersViewHolder extends RecyclerView.ViewHolder
{
View mView;
public AllUsersViewHolder(View itemView)
{
super(itemView);
mView = itemView;
}
public void setUser_name(String user_name)
{
TextView name = (TextView) mView.findViewById(R.id.all_users_username);
name.setText(user_name);
}
public void setUser_status(String user_status)
{
TextView status = (TextView) mView.findViewById(R.id.all_users_status);
status.setText(user_status);
}
public void setUser_image(Context ctx, String user_image)
{
CircleImageView image = (CircleImageView) mView.findViewById(R.id.all_users_profile_image);
Picasso.with(ctx).load(user_image).into(image);
}
}
}
Response_AllUsers:
public class Response_AllUsers {
public String user_name;
public String user_image;
public String user_status;
public Response_AllUsers()
{
}
public Response_AllUsers(String user_name, String user_image, String user_status) {
this.user_name = user_name;
this.user_image = user_image;
this.user_status = user_status;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_image() {
return user_image;
}
public void setUser_image(String user_image) {
this.user_image = user_image;
}
public String getUser_status() {
return user_status;
}
public void setUser_status(String user_status) {
this.user_status = user_status;
}
}
You are using a new version of Firebaseui, then inside
onStart()
you need to add the following:to start listening for data
and inside
onStop()
, add:more info here:
Firebase ui lifecycle