I following this Tutorial series on building chat app in android studio and I build a recycle view attached with Firebase database but It’s not showing up. You can see in this image, It only shows the area recycle view takes in dull white instead of showing list.
Here is my Firebase database view:
Firebase database structure
Realtime-database image Realtime-database image
Authentication tab image Authentication data image
// In MainActivity
DatabaseReference reference = database.getReference().child("User");
// in Register.java
String id = task.getResult().getUser().getUid();
DatabaseReference reference = database.getReference().child("user").child(id);
StorageReference storageReference = storage.getReference().child("upload").child(id);
Here is: MainActivity.java: Where Recycle View is Present
package com.example.javamassenger;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import com.google.firebase.Firebase;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
FirebaseAuth auth;
RecyclerView UserRecyclerView;
userAdapter adapter;
FirebaseDatabase database;
ArrayList<Users> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference().child("User");
auth = FirebaseAuth.getInstance();
arrayList = new ArrayList<>();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot: snapshot.getChildren())
{
Users users = dataSnapshot.getValue(Users.class);
// adding data in array
arrayList.add(users);
Toast.makeText(MainActivity.this, "It's working", Toast.LENGTH_SHORT).show();
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(MainActivity.this, "Can't Access Data", Toast.LENGTH_SHORT).show();
}
});
UserRecyclerView = findViewById(R.id.mainUserRecyclerView);
UserRecyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new userAdapter(MainActivity.this, arrayList);
UserRecyclerView.setAdapter(adapter);
}
}
Here is: Adapter as userAdapter.java
package com.example.javamassenger;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class userAdapter extends RecyclerView.Adapter<userAdapter.viewholder>{
Context mainActivity;
ArrayList<Users> arrayList;
public userAdapter(MainActivity mainActivity, ArrayList<Users> arrayList) {
this.mainActivity = mainActivity;
this.arrayList = arrayList;
}
@NonNull
@Override
public userAdapter.viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mainActivity).inflate(R.layout.users_item, parent,false);
return new viewholder(view);
}
@Override
public void onBindViewHolder(@NonNull userAdapter.viewholder holder, int position) {
Users users = arrayList.get(position);
holder.username.setText(String.valueOf(users.userName));
holder.status.setText(String.valueOf(users.status));
Picasso.get().load(users.profilepic).into(holder.circleImageView);
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class viewholder extends RecyclerView.ViewHolder {
CircleImageView circleImageView;
TextView status, username;
public viewholder(@NonNull View itemView) {
super(itemView);
circleImageView = itemView.findViewById(R.id.cirImg);
status = itemView.findViewById(R.id.status);
username = itemView.findViewById(R.id.username);
}
}
}
Setters & Geters Class For Array: Users.java
public class Users {
String profilepic, mail, userName, password, userId, lastMessage, status;
public Users(){}
public Users(String userId, String userName, String mail, String password, String profilepic, String status) {
this.userId = userId;
this.userName = userName;
this.mail = mail;
this.password = password;
this.profilepic = profilepic;
this.status = status;
}
public String getProfilepic() {
return profilepic;
}
public void setProfilepic(String profilepic) {
this.profilepic = profilepic;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getLastMessage() {
return lastMessage;
}
public void setLastMessage(String lastMessage) {
this.lastMessage = lastMessage;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
MainActivity XML file activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mainback"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/toolBar"
android:layout_width="wrap_content"
android:layout_height="@dimen/_55sdp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="@dimen/_23sdp"
android:layout_marginLeft="@dimen/_22sdp"
android:layout_marginTop="@dimen/_12sdp"
android:layout_marginEnd="@dimen/_26sdp"
android:layout_marginRight="@dimen/_25sdp"
android:background="@color/white"
android:gravity="right">
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mainUserRecyclerView"
android:layout_width="334dp"
android:layout_height="330dp"
android:layout_below="@id/toolBar"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="13dp"
android:layout_marginTop="160dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="107dp" />
users_items.xml layout view for recycle view to show items
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bordermain"
android:layout_margin="13.00dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/cirImg"
android:layout_width="114dp"
android:layout_height="80dp"
android:layout_marginStart="13.00dp"
android:layout_marginTop="13.00dp"
android:layout_marginEnd="13.00dp"
android:layout_marginBottom="@dimen/_10sdp"
android:src="@drawable/photocamera" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_bold"
android:text="Name"
android:textColor="@color/black"
android:textSize="23.40dp"/>
<TextView
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:fontFamily="@font/poppins_regular"
android:textSize="10.40dp"
android:text="Status"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
If anything else is need to understand problem properly please ask. Any help is appreciated.
2
Answers
You are updating your array list which is present as a variable inside your MainActivity.java. That update is not noticed by the adapter. Either make a function inside the adapter which takes that updated array list and updates the list (which is local to the adapter) and then calls notifyDataSetChanged.
Or reinitialize your adapter every time your arrayList is updated, which is not a better way to do it. I recommend using the first approach.
The problem lies in the following line of code:
Where your child is called
User
, with capitalU
, while in your database, is written in lowercase:To solve this, simply change the starting letter to
u
: