0

Refresh effect to the FirebaseRecyclerAdapter while typing in the text field that loads data in an Autocomplete manner. I used to call the the following function firebaseUserSearch withe help of onTextChanged method to fill the adapter. At each time when the firebaseUserSearch is being called it refreshes the recylerview which i don't want , if the result is already present there.

private void firebaseUserSearch(String searchText) {
    Query firebaseSearchQuery = mUserDatabase.orderByChild("name").startAt(searchText).endAt(searchText + "\uf8ff");
    FirebaseRecyclerAdapter<Users, SearchActivity.UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, SearchActivity.UsersViewHolder>(
            Users.class,
            R.layout.list_layout,
            SearchActivity.UsersViewHolder.class,
            firebaseSearchQuery
    ) {
        @Override
        protected void populateViewHolder(SearchActivity.UsersViewHolder viewHolder, final Users model, final int position) {
            viewHolder.setDetails(getActivity().getApplicationContext(), model.getName(), model.getPlace(), model.getImage());
            viewHolder.relLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), ResultActivity.class);
                    intent.putExtra("name", model.getName());
                    intent.putExtra("place", model.getPlace());
                    intent.putExtra("image", model.getImage());
                    startActivity(intent);

                }
            });
        }
    };
    mResultList.setAdapter(firebaseRecyclerAdapter);
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mohammed Javad
  • 163
  • 1
  • 11

2 Answers2

1

onTextChanged will fire evey time you type something in the textfield.

You might want to use afterTextChanged.

Here is an example -

editText.addTextChangedListener(
    new TextWatcher() {
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        private Timer timer=new Timer();
        private final long DELAY = 500; // milliseconds

        @Override
        public void afterTextChanged(final Editable s) {
            timer.cancel();
            timer = new Timer();
            timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        // TODO: do what you need here (refresh list)
                        // you will probably need to use runOnUiThread(Runnable action) for some specific actions
                    }
                }, 
                DELAY
            );
        }
    }
);
Galib Imtiaz
  • 106
  • 2
  • 7
1

Every time you change the search term you create a new query and a new adapter, which leads to a complete reload (and repaint) of the data. The standard FirebaseRecyclerAdapter has no way to prevent this.

If you want a more intelligent adapter, you will have to create it yourself. You'll have to compare each item you get back from the query with items you already have, and only pass the actual changes along to your adapter.

While that is definitely possible, it is quite some work. If you get stuck along the way, feel free to post a question that reproduces where you are stuck.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807