-1

Searching Option is not working properly in dialog fragments.Though i had used same in other app in activity.I am using Recyclerview.

Here is my code in Custom Adapter:

public void filter(final String text) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            filterlist.clear();
            if (TextUtils.isEmpty(text)) {

                filterlist.addAll(bank);

            } else {
                for (Employee item : bank) {
                    if (item.getBank_name().toLowerCase().contains(text.toLowerCase())) {
                        // Adding Matched items
                        filterlist.add(item);

                    }
                }
            }

            ((Activity) context).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    notifyDataSetChanged();

                }
            });

        }
    }).start();
}

Error is in ((Activity) context).runOnUiThread(new Runnable() ) this line

Logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.runOnUiThread(java.lang.Runnable)' on a null object reference
                                                                             at com.example.abhishek.project.CustomAdapter$1.run(CustomAdapter.java:75)

recyclerview:

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    try{
        if(TextUtils.isEmpty(newText)){
            ca.filter("");
        }
        else{
            ca.filter(newText.toString());
        }

private void setupSearchView() {
    search.setIconifiedByDefault(false);
    search.setOnQueryTextListener(this);
    search.setSubmitButtonEnabled(true);
    search.setQueryHint("Search Here");

}
Abhi
  • 385
  • 1
  • 4
  • 13
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – earthw0rmjim Sep 06 '16 at 05:59
  • But there is no error when we remove runonuithread. – Abhi Sep 06 '16 at 06:05
  • Adding items in the list and calling `notifyDatasetChanged()` should be done in the activity or fragment where adapter is getting initialized. – Faraz Sep 06 '16 at 06:29
  • use [this](https://gist.github.com/pskink/cd3bbdd742b5b1905a790c76831b5d85) generic adapter – pskink Sep 06 '16 at 06:36

1 Answers1

0

You have get Activity context like this in DialogFragment:

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        notifyDataSetChanged();
    }
});
earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51