user4165123
user4165123

Reputation: 183

Recycler view not updating data with Realm data

I have a recycler view where I list a set of names. Above the recycler view, I have a search field where the user should be able to search for a specific name, and the recycler view should update as they type to show only the relevant results.

Here is my class:

public class MyFragment extends Fragment {

    private EditText searchField;

    private RecyclerView recyclerView;

    private MyAdapter adapter;

    private Realm realm;

    private RealmResults<Person> persons;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        realm = Realm.getDefaultInstance();

        return inflater.inflate(R.layout.fragment_persons, parent, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        searchField = view.findViewById(R.id.search_field);
        recyclerView = view.findViewById(R.id.recycler_view);

        searchField.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String query = searchField.getText().toString();
                fetchPersons(query);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());

        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        fetchPersons("");

        adapter = new MyAdapter(getContext(), persons);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if (realm != null) {
            realm.close();
            realm = null;
        }
    }

    private void fetchPersons(String query) {
        persons = realm.where(Person.class)
                .contains("name", query)
                .findAll();
    }

}

However, calling adapter.notifyDataSetChanged(); inside onTextChanged doesn't actually update the recycler view, so nothing ends up changing.

What am I doing wrong?

Upvotes: 0

Views: 582

Answers (2)

Priyanka
Priyanka

Reputation: 1875

You need to create a method in adapter class to update adapter. Something as below -

   public void updateData(RealmResults<Person> personList) {
    this.persons = personList;
    notifyDataSetChanged();
}

Suggestion - We should keep adapter variables private and update these variable using functions only. Also notifyDataSetChanged() should called within the adapter or its functions.

Upvotes: 1

McSlinPlay
McSlinPlay

Reputation: 549

I think that you will need to set the data in your adapter.

adapter.persons.clear();
adapter.addAll(persons);
adapter.notifyDataSetChanged();

Upvotes: 0

Related Questions