Reputation: 73
I want to show me all the users from firebase databse in a cardview who is in a recyclerview but in debugger shows me an error at line 60 in UserActivity.
I put a screenshot with my database:
The UsersFragment:
public class UsersFragment extends Fragment {
private RecyclerView recyclerView;
private UserAdapter userAdapter;
private List<User> mUsers;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate ( R.layout.fragment_users3, container, false );
recyclerView=view.findViewById (R.id.recycler_view );
recyclerView.setLayoutManager ( new LinearLayoutManager ( getContext () ) );
mUsers=new ArrayList<> ( );
readUsers();
return view;
}
private void readUsers () {
final FirebaseUser firebaseUser= FirebaseAuth.getInstance ().getCurrentUser ();
DatabaseReference referenc= FirebaseDatabase.getInstance ().getReference ("users");
referenc.addValueEventListener ( new ValueEventListener () {
@Override
public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
mUsers.clear ();
for(DataSnapshot snapshot:dataSnapshot.getChildren ()){
User user=snapshot.getValue (User.class);
assert user!=null;
assert firebaseUser!=null;
if(!firebaseUser.getUid ().equals ( user.getUid () )){
mUsers.add ( user );
}
}
userAdapter=new UserAdapter ( getContext (),mUsers );
recyclerView.setAdapter ( userAdapter );
}
@Override
public void onCancelled (@NonNull DatabaseError databaseError) {
}
} );
}
}
The UserAdapter.In i used the User class:
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private Context mContext;
private List<User> mUsers;
public UserAdapter(Context mContext,List<User> mUsers){
this.mUsers=mUsers;
this.mContext=mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from ( mContext ).inflate ( R.layout.users_template ,parent,false);
return new UserAdapter.ViewHolder ( view );
}
@Override
public void onBindViewHolder (@NonNull ViewHolder holder, int position) {
User user=mUsers.get ( position );
holder.username.setText ( user.getFirstName () );
}
@Override
public int getItemCount () {
return mUsers.size ();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView username;
public ViewHolder(View itemView){
super(itemView);
username=itemView.findViewById ( R.id.username1 );
}
}
}
The User that stores the FirstName :
public class User {
public String firstName;
public String secondName;
public String uid;
public String email;
public User(){
}
public User (String firstName, String secondName, String uid, String email) {
this.firstName = firstName;
this.secondName = secondName;
this.uid=uid;
this.email=email;
}
public User (String firstname, String secondname) {
}
@PropertyName ( "FirsName" )
public String getFirstName () {
return firstName;
}
@PropertyName ("SecondName" )
public String getSecondName () {
return secondName;
}
@PropertyName ( "Uid" )
public String getUid () {
return uid;
}
@PropertyName ( "E-mail" )
public String getEmail () {
return email;
}
@PropertyName ( "FirsName" )
public void setFirstName (String firstName) {
this.firstName = firstName;
}
@PropertyName ( "SecondName" )
public void setSecondName (String secondName) {
this.secondName = secondName;
}
@PropertyName ( "Uid" )
public void setUid (String uid) {
this.uid = uid;
}
@PropertyName ( "E-mail" )
public void setEmail (String email) {
this.email = email;
}
}
The rules for my database are:
{
"rules": {
".read": "true",
".write":"true",
"users":{
"$user_id":{
".read": "auth.uid === $user_id",
".write": "auth.uid === $user_id"
}
}
}
}
In the debug mode it shows me this:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sportsbuddy, PID: 662
com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: secondname
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.addProperty(com.google.firebase:firebase-database@@17.0.0:540)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.<init>(com.google.firebase:firebase-database@@17.0.0:471)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-database@@17.0.0:312)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@17.0.0:413)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@17.0.0:214)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@17.0.0:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@17.0.0:212)
at com.example.sportsbuddy.Fragments.UsersFragment$1.onDataChange(UsersFragment.java:60)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6986)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
The main error is at onDataChange in UsersFragment.
I expect to show me all the users that I have in the database.
Upvotes: 0
Views: 407
Reputation: 598837
Both your fields and your getters/setters are public. This means that Firebase sees two properties:
secondname for the field
SecondName for the getter/setter
Since it's now unclear how to map the Java class to the JSON from the database, it throws this error:
Found two getters or fields with conflicting case sensitivity for property
The simplest fix is to make your fields private:
public class User {
private String firstName;
private String secondName;
private String uid;
private String email;
public User(){
}
...
Upvotes: 0
Reputation: 3712
Your error says:
Found two getters or fields with conflicting case sensitivity for property: secondname
For understanding what this error means, you need to understand how Firebase serializes & deserializes the data that you receive from your database.
Firebase uses field names, as well as getters and setters to serialize and deserialize object, which means, whenever you receive JSON object from your database, firebase will map these key values with your getters/setters as well as the fields in your POJO class.
To remove this error, you'll have to either change your field names, i.e.
public String secondName;
to something different than that you have in your database, OR , you need to change the getter & setter function names. Error is given as firebase is seeing the property twice, once in your fields, and second time in your getter/setter.
Also, try making your variables private. It might help. Also, how about changing "E-mail" in your database to simply "email"?
Upvotes: 2
Reputation: 739
With out knowing the exact error would be difficult to say.
But i think is when you convert the firebase object to user class. In your firebase the field email is"e-mail" and in your class is "email". Change it to match your firebase database or the reverse (change the firebase field to match your user class).
Post your complete error in order for us to be able to provide more information.
---Edit---
Its the case sensitivity between the firebase database and your user class. Change the fields to match each other and you will be fine.
Upvotes: 0
Reputation: 658
Change your constructor from
public User (String firstname, String secondname) {
}
to
public User (String firstName, String secondName) {
}
Also, your firebase key doesn't match with your model class key.
Your FirebaseKey is FirstName
and your model class key is firstName
. Fix this(same for SecondName
vs secondName
)!
Upvotes: 0