Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

Using FirebaseListAdapter to display a list of User


I'm trying to add a "Search User" Activity to a simple App I'm trying to make. But I'm having trouble taking the user input (from a SearchView) and using it to make a query into the Firebase Database to finally display everything in a Listview (using FirebaseListAdapter).

The problem is mainly with the Firebase part that I can't really figure out. Here is the code:

SearchView userSearchView;
ListView userListView;
SearchUserListAdapter UserAdapter;
String currentUserName;
String currentEnteredQuery = null;


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

    userSearchView = (SearchView) findViewById(R.id.user_searchview);
    userListView = ( ListView) findViewById(R.id.listview_search);
    final Intent startConversation = new Intent(this,ConversationActivity.class);

    //Start a new conversation
    userListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startActivity(startConversation);
        }
    });

    Query query = UserDbRef.orderByChild(currentEnteredQuery);
    final FirebaseListAdapter firebaseListAdapter = new FirebaseListAdapter<UserItem>(this, UserItem.class, R.layout.user_item, query) {
        @Override
        protected void populateView(View v, UserItem userItem) {
            ((TextView)v.findViewById(android.R.id.text1)).setText(userItem.getUser_name());
        }
    };
    userListView.setAdapter(firebaseListAdapter);


    userSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String inputQuery) {
            currentEnteredQuery = inputQuery;
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_search_user, menu);
    return true;
}

Solution

  • Put your query inside the Listener for the SearchView so that it would query everytime you search with a specific criteria:

    userSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    
        @Override
        public boolean onQueryTextSubmit(String inputQuery) {
            currentEnteredQuery = inputQuery;
            //Your Firebase Query here
            Query query = UserDbRef.orderByChild(currentEnteredQuery);
            final FirebaseListAdapter firebaseListAdapter = new FirebaseListAdapter<UserItem>(this, UserItem.class, R.layout.user_item, query) {
                @Override
                protected void populateView(View v, UserItem userItem) {
                    ((TextView)v.findViewById(android.R.id.text1)).setText(userItem.getUser_name());
                }
            };
            userListView.setAdapter(firebaseListAdapter);
            return true;
        }
    
        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    

    Also in these lines:

    final FirebaseListAdapter firebaseListAdapter = new FirebaseListAdapter<UserItem>(this, UserItem.class, R.layout.user_item, query) {
        @Override
        protected void populateView(View v, UserItem userItem) {
            ((TextView)v.findViewById(android.R.id.text1)).setText(userItem.getUser_name());
        }
    };
    

    You are using R.layout.user_item as your layout for the list but you are using android.R.id.text1. As far as I know, android.R are resources built-in the Android SDK. I think this is the view if you are using android.R.layout.simple_list_item_1. Go ahead and check your R.layout.user_item and change the id to the corresponding view in your layout.