Issue
I am not able to retrieve data from Firebase it always crashes saying:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.whatsappclone.User
This is My Model Class
public class User {
String uid, name, phoneNumber, profileImage;
public User()
{
}
public User(String uid, String name, String phoneNumber, String profileImage) {
this.uid = uid;
this.name = name;
this.phoneNumber = phoneNumber;
this.profileImage = profileImage;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getProfileImage() {
return profileImage;
}
public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
}
}
This is My ViewHolder Class
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UsersViewHolder> {
Context context;
ArrayList<User> users;
public UsersAdapter(Context context, ArrayList<User>users) {
this.context = context;
this.users = users;
}
@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.conversation_layout, parent, false);
return new UsersViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull UsersViewHolder holder, int position) {
User user = users.get(position);
holder.binding.userName.setText(user.getName());
Glide.with(context).load(user.getProfileImage())
.into(holder.binding.profile);
}
@Override
public int getItemCount() {
return users.size();
}
public class UsersViewHolder extends RecyclerView.ViewHolder {
ConversationLayoutBinding binding;
public UsersViewHolder(@NonNull View itemView) {
super(itemView);
binding = ConversationLayoutBinding.bind(itemView);
}
}
}
This is My MainActivity
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
FirebaseDatabase database;
ArrayList<User> users;
UsersAdapter usersAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
database = FirebaseDatabase.getInstance();
users = new ArrayList<>();
usersAdapter = new UsersAdapter(this, users);
binding.recyclerView.setAdapter(usersAdapter);
database.getReference().child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
users.clear();
for (DataSnapshot snapshot1 : snapshot.getChildren())
{
User user = snapshot1.getValue(User.class);
users.add(user);
}
usersAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
My Firebase database Structured
Firebase JSON
{ "users" : {
"A5DEg64d47StvLuvRtLWiu1bHSC3" : {
"name" : "Rahul Singh",
"phoneNumber" : "+918350877517",
"profileImage" : "https://firebasestorage.googleapis.com/v0/b/whatsapp-clone-b908d.appspot.com/o/Profiles%2FA5DEg64d47StvLuvRtLWiu1bHSC3?alt=media&token=55fcdc75-da0c-43f5-8f9c-44bfa7df500f",
"uid" : "A5DEg64d47StvLuvRtLWiu1bHSC3"
},
"CO6Yw59BHweNJ03UdbcsGq4kyXY2" : {
"name" : "Alok Bhai",
"phoneNumber" : "+919886542409",
"profileImage" : "https://firebasestorage.googleapis.com/v0/b/whatsapp-clone-b908d.appspot.com/o/Profiles%2FCO6Yw59BHweNJ03UdbcsGq4kyXY2?alt=media&token=b006e40d-e8df-4b96-9165-d980cd018af3",
"uid" : "CO6Yw59BHweNJ03UdbcsGq4kyXY2"
},
"F8mhqJk4POeLFWxpF373K2D8DYG2" : {
"name" : "radhe govind'",
"phoneNumber" : "+916378757808",
"profileImage" : "No Image",
"uid" : "F8mhqJk4POeLFWxpF373K2D8DYG2"
},
"NOt57HC59DZm1s5XaBk0mHb9H553" : {
"name" : "Mohit Dada💪",
"phoneNumber" : "+917853471027",
"profileImage" : "https://firebasestorage.googleapis.com/v0/b/whatsapp-clone-b908d.appspot.com/o/Profiles%2FNOt57HC59DZm1s5XaBk0mHb9H553?alt=media&token=6aba255c-5f96-4ba1-a4a4-b998d3ae5b4a",
"uid" : "NOt57HC59DZm1s5XaBk0mHb9H553"
},
"XnJUq8LzauZPCLCi5v4SeCGgSPa2" : {
"name" : "Alok Singh Deshwal",
"phoneNumber" : "+919960726182",
"profileImage" : "No Image",
"uid" : "XnJUq8LzauZPCLCi5v4SeCGgSPa2"
},
"ZIcwyjMGC3Yk5GrT7vIeTxphQPt2" : {
"name" : "Anku Bhai",
"phoneNumber" : "+919094974311",
"profileImage" : "https://firebasestorage.googleapis.com/v0/b/whatsapp-clone-b908d.appspot.com/o/Profiles%2FZIcwyjMGC3Yk5GrT7vIeTxphQPt2?alt=media&token=5773be33-d610-4f8d-8bbd-d655e5228666",
"uid" : "ZIcwyjMGC3Yk5GrT7vIeTxphQPt2"
},
"kE3i5DsP9DTorG5DkJ4Vy7nArwZ2" : {
"name" : "Prinshu",
"phoneNumber" : "+91989600555",
"profileImage" : "https://firebasestorage.googleapis.com/v0/b/whatsapp-clone-b908d.appspot.com/o/Profiles%2FkE3i5DsP9DTorG5DkJ4Vy7nArwZ2?alt=media&token=e4d13d95-3906-4bdf-aaac-028816e490d5",
"uid" : "kE3i5DsP9DTorG5DkJ4Vy7nArwZ2"
},
"name" : "Vivek Fauzdar",
"phoneNumber" : "+916486627517",
"profileImage" : "https://firebasestorage.googleapis.com/v0/b/whatsapp-clone-b908d.appspot.com/o/Profiles%2FA5DEg64d47StvLuvRtLWiu1bHSC3?alt=media&token=cf8613fb-ae66-4892-bd79-4affa89d86e4",
"uid" : "A5DEg64d47StvLuvRtLWiu1bHSC3",
"vYfKxxkvoCV0p4c4P64t3F1IrEn1" : {
"name" : "Om Swami",
"phoneNumber" : "+919887012345",
"profileImage" : "No Image",
"uid" : "vYfKxxkvoCV0p4c4P64t3F1IrEn1"
}
} }
Solution
You are getting the following error:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.whatsappclone.User
Because under your "users" there aren't only User objects, there are also strings:
"name" : "Vivek Fauzdar",
"phoneNumber" : "+916486627517",
"profileImage" : "https://firebasestorage.googleapis.com/v0/b/whatsapp-clone-b908d.appspot.com/o/Profiles%2FA5DEg64d47StvLuvRtLWiu1bHSC3?alt=media&token=cf8613fb-ae66-4892-bd79-4affa89d86e4",
"uid" : "A5DEg64d47StvLuvRtLWiu1bHSC3",
When you are using the following line of code:
User user = snapshot1.getValue(User.class);
It means that you are trying to map each child into an object of type User
, which isn't possible since you are mixing User
objects with strings, hence that error. The simplest solution would be to remove all those strings and make sure you have only User
objects under the "users" node. The code looks good to me.
See, all these children exist right under the "users" node.
Answered By - Alex Mamo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.