1

I would like to search Users from Firebase by their first name and display every user with identical first names in the RecyclerView along with their last name and sex.

I got the code from a youtube tutorial(https://www.youtube.com/watch?v=b_tz8kbFUsU) but the tutorial used an older version of FirebaseUI and I am using the 3.2.2 version.

I tried to update the older version code by whatever came to my mind but I don't know... so there are probably some errors due to that too here.

Gradle Build is successful. But I am not getting the list when I click the search button. And the logcat shows the error No adaper attached skipping layout when Search button is clicked.

Here is the activity code

private EditText SearchField;
private Button SearchButton;



private RecyclerView ResultList;

private DatabaseReference UserDatabase;


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

    UserDatabase = FirebaseDatabase.getInstance().getReference().child("Users");

    SearchField = (EditText) findViewById(R.id.Name);
    SearchButton = (Button) findViewById(R.id.Search);


    ResultList = (RecyclerView) findViewById(R.id.result_list);
    ResultList.setHasFixedSize(true);
    ResultList.setLayoutManager(new LinearLayoutManager(this));


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

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

            firebaseUserSearch(searchText);
        }
    });

}

private void firebaseUserSearch(String searchText){
    super.onStart();

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

    Query firebaseSearchQuery = UserDatabase.orderByChild("firstName").startAt(searchText).endAt(searchText + "\uf8ff");

    FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>().setQuery(firebaseSearchQuery, Users.class).build();
    FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {

        @Override
        protected void onBindViewHolder(UsersViewHolder viewHolder, int position, Users model) {
            viewHolder.setDetails(model.getFirstName(), model.getLastName(), model.getSex());

        }

        @Override
        public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.user_list, parent, false);

            return new UsersViewHolder(view);
        }
    };


    ResultList.setAdapter(firebaseRecyclerAdapter);


}

public static class UsersViewHolder extends RecyclerView.ViewHolder{

    View view;

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

        view = itemView;

    }

    public void setDetails(String userfirstname, String userlastname, String userSex){

        TextView user_firstname = (TextView) view.findViewById(R.id.textFirstName);
        TextView user_lastname = (TextView) view.findViewById(R.id.textLastName);
        TextView user_sex = (TextView) view.findViewById(R.id.textSex);

        user_firstname.setText(userfirstname);
        user_lastname.setText(userlastname);
        user_sex.setText(userSex);
    }
}

Here is the Users class code

public String firstName, lastName, Sex;

public void Intructors(){

}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getSex() {
    return Sex;
}

public void setSex(String sex) {
    this.Sex = sex;
}

public Users(String firstName, String lastName, String sex) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.Sex = sex;
}

Here is the xml code for the User_list

<?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="match_parent">

<TextView
    android:id="@+id/textFirstName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="44dp"
    android:layout_marginStart="44dp"
    android:layout_marginTop="8dp"
    android:text="Fist Name"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

<TextView
    android:id="@+id/textSex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textFirstName"
    android:layout_alignStart="@+id/textFirstName"
    android:layout_below="@+id/textFirstName"
    android:layout_marginTop="35dp"
    android:text="Sex"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

<TextView
    android:id="@+id/textLastName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textFirstName"
    android:layout_marginLeft="43dp"
    android:layout_marginStart="43dp"
    android:layout_toEndOf="@+id/textFirstName"
    android:layout_toRightOf="@+id/textFirstName"
    android:text="Last Name"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1" />
</RelativeLayout>

This is what the activity xml looks like

https://i.stack.imgur.com/Q6tka.jpg

And finally here is my firebase database structure

https://i.stack.imgur.com/jeL01.jpg

Please help me find the error. I welcome every suggestion.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Color Blue
  • 11
  • 2
  • Have you tried to comment this line: `ResultList.setHasFixedSize(true);`? – Alex Mamo Mar 14 '18 at 13:12
  • Possible duplicate of [Output not showing when the firebase ui upgrade to 3.2.2](https://stackoverflow.com/questions/49080710/output-not-showing-when-the-firebase-ui-upgrade-to-3-2-2) – Alex Mamo Mar 14 '18 at 13:16
  • I don't know. I can't seem to find the error. Do you think that is the reason for the No adapter attached error? – Color Blue Mar 14 '18 at 13:55
  • Have you followed those steps? – Alex Mamo Mar 14 '18 at 13:56
  • Yes. But where do I place the onStart method because it says it can't resolve firebaseRecyclerAdapter? – Color Blue Mar 14 '18 at 13:58
  • Take also a look [here](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android/49277842). It's for Firestore but much the same. – Alex Mamo Mar 14 '18 at 14:03
  • Sorry, I still can't figure out where to place the onStart and onStop method... – Color Blue Mar 14 '18 at 15:36

1 Answers1

0

You need to tell the adapter to start listening for changes. The easiest way to do that is to call FirebaseRecyclerOptions.Builder#setLifecycleOwner(this), or you could manually decide when to start and stop listening by calling FirebaseRecyclerAdapter#[start/stop]Listening(). I'd recommend checking out the docs.

SUPERCILEX
  • 3,929
  • 4
  • 32
  • 61