5

Hi everyone I try to retrieve data from Firebase dataset and I keep getting this error even tho I tried every single solution from StackOverflow and what I was able to find on Google but without any benefit.

Below I will link the Activity and the Class and the datasets.

I'm new to Android and I can't understand why is not working since some people solved the problem and I did the same thing but nothing worked.

Thank you in advance.

CODE ACTIVITY

package com.example.ipill;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;

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.GenericTypeIndicator;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class FirebaseSearch extends AppCompatActivity {

    private EditText mSearchField;
    private ImageButton mSearchBtn;

    private RecyclerView mResultList;



    private DatabaseReference mUserDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_firebasesearch);

        mUserDatabase = FirebaseDatabase.getInstance().getReference("Users");

        mSearchField = (EditText) findViewById(R.id.search_field);

        mSearchBtn = findViewById(R.id.search_btn);
        mResultList = (RecyclerView)findViewById(R.id.result_list);

        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));

        mSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String searchText = mSearchField.getText().toString();

                firebaseUserSearch(searchText);

            }
        });


    }

    private void firebaseUserSearch(String searchText) {

        Toast.makeText(FirebaseSearch.this, "Started Search", Toast.LENGTH_LONG).show();

        Query firebaseSearchQuery = mUserDatabase.orderByChild("Name").startAt(searchText).endAt(searchText + "\uf8ff");

        FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(

                Users.class,
                R.layout.list_layout,
                UsersViewHolder.class,
                firebaseSearchQuery

        ) {
            @Override
            protected void populateViewHolder(UsersViewHolder viewHolder, Users model, int position) {
                viewHolder.setDetails(model.getName(), model.getSurname());

            }
        };

        mResultList.setAdapter(firebaseRecyclerAdapter);

    }


    // View Holder Class

    public static class UsersViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public UsersViewHolder(View itemView) {
            super(itemView);
            mView = itemView;

        }

        public void setDetails(String name, String surname) {

            TextView user_name = (TextView) mView.findViewById(R.id.name_text);
            TextView user_surname = (TextView) mView.findViewById(R.id.status_text);

            user_name.setText(name);
            user_surname.setText(surname);


        }

    }
}

DATABASE:

{
  "Users" : {
    "01" : {
      "Name" : "Alex",
      "Surname" : "Kyto"
    }
  }
}

The problem that I have:

No setter/field for Surname found on class com.example.ipill.Users (fields/setters are case sensitive!) No setter/field for Name found on class com.example.ipill.Users (fields/setters are case sensitive!)

Model CLASS

package com.example.ipill;

public class Users {

    public String name, surname;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getSurname() {
        return surname;
    }


    public void setSurname(String surname) {
        this.surname = surname;
    }


    public Users(String name, String surname, String status) {
        this.name = name;
        this.surname = surname;

    }

    public Users(){

    }

} 
Alex97
  • 401
  • 1
  • 8
  • 21

3 Answers3

5

To be able to map your POJO class to the database, it needs to have the same variable names, also the instance variables are case sensitive. The problem is that in your database, you have Name and Surname with capital letter while in the POJO class you have them with lowercase letter.

Therefore, you need to change your database to the following:

{
  "Users" : {
    "01" : {
      "name" : "Alex",
      "surname" : "Kyto"
    }
  }
}
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
3

Change your Users property like below to match with Firebase. You database and POJO should align to work with.

public String Name, Surname; //Capitalize each property
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
2

I was getting this error because I was accessing wrong variable(parent variable)

public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                   list.add(snapshot.getValue(CategoryModel.class));
                   // Here in list I should have added dataSnapshot.getValue(CategoryModel.class)
               }

            
}

Hope it helps.