2

So the problem is that when I type in something it just won't search. I need it to do basic search like when I type a letter or number it will filter it.

This is my Adapter class:

public class KnjigeSearchAdapter extends ArrayAdapter<Knjiga> implements Filterable{
    Context context;
    private ArrayList<Knjiga> originalValues;
    private NameFilter filter;
    ArrayList<Knjiga> podaci = new ArrayList<>();

    public KnjigeSearchAdapter(Context context,ArrayList<Knjiga> knjigas){
        super(context, 0, knjigas);
        this.context = context;

        this.podaci = knjigas;
        this.originalValues = new ArrayList<>();
        this.originalValues.addAll(podaci);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Knjiga knjiga = getItem(position);

        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.search_knjiga_layout, parent, false);
        }
        TextView tvnazivK = (TextView)convertView.findViewById(R.id.textViewNazivKnjige);
        TextView tvautorK = (TextView)convertView.findViewById(R.id.textViewAutorKnjige);
        TextView tvisbn = (TextView)convertView.findViewById(R.id.textViewIsbnKnjige);
        TextView tvGodina = (TextView)convertView.findViewById(R.id.textViewGodinaIzdKnjige);
        TextView tvkatK = (TextView)convertView.findViewById(R.id.textViewKategorijaKnjige);

        tvnazivK.setText(knjiga.getNaziv());
        tvautorK.setText(knjiga.getAutor());
        tvisbn.setText(knjiga.getIsbn());
        tvGodina.setText(String.valueOf(knjiga.getGodinaIzdavanja()));
        tvkatK.setText(knjiga.getKategorija());

        return convertView;
    }

    @Override
    public Filter getFilter() {
        if(filter == null){
            filter = new NameFilter();
        }
        return filter;
    }
    private class NameFilter extends Filter{
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = constraint.toString().toLowerCase();
            FilterResults results = new FilterResults();
            if(constraint != null && constraint.toString().length() > 0)
            {
                ArrayList<Knjiga> filtrirano = new ArrayList<>();

                for(int i = 0, l = originalValues.size(); i < l; i++)
                {
                    Knjiga knjiga = originalValues.get(i);
                    if(knjiga.toString().toLowerCase().contains(constraint))
                        filtrirano.add(knjiga);
                }
                results.count = filtrirano.size();
                results.values = filtrirano;
            }
            else
            {
                synchronized(this)
                {
                    results.values = originalValues;
                    results.count = originalValues.size();
                }
            }
            return results;
        }
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {

            podaci = (ArrayList<Knjiga>)results.values;
            notifyDataSetChanged();
            clear();
            for(int i = 0, l = podaci.size(); i < l; i++)
                add(podaci.get(i));
            notifyDataSetInvalidated();
        }
    }
        }

This is my Activity class:

public class KorisnikSearchKnjigeActivity extends AppCompatActivity {
    ListView lvPretragaknjiga;
    SearchView svSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_korisnik_search_knjige);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        pretragaKnjiga();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void pretragaKnjiga() {
        lvPretragaknjiga = (ListView) findViewById(R.id.listViewListaKnjiga);
        svSearch = (SearchView) findViewById(R.id.searchViewSearch);

        MyDBHelper helper = new MyDBHelper(this);
        final KnjigeSearchAdapter adapter = new KnjigeSearchAdapter(this, helper.getAllKnjige());
        lvPretragaknjiga.setAdapter(adapter);

        svSearch.setOnQueryTextListener(
                new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        return false;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        adapter.getFilter().filter(newText);
                        return true;
                    }
                }
        );

    }
}
Gavriel
  • 18,088
  • 12
  • 63
  • 98

1 Answers1

0

The problem might be that you're filtering by comparing Kijiga.toString() to what you typed, and probably it doesn't have the "content" string you think it has. See what toString returns, probably something like: "[Kijiga#1345]". Instead of toString, you might want to use some getter. Or override toString.

Also could try to change:

    public boolean onQueryTextSubmit(String query) {
          adapter.getFilter().filter(query);
          return true;
    }

And IMHO there's a bug in the emulator, so it will not always work (depending on the HW keyboard settings), so test it on a real device!

Gavriel
  • 18,088
  • 12
  • 63
  • 98
  • can you add a breakpoint to NameFilter.performFiltering on the 1st line, and see if you get there at least? If yes, then move the breakpoint to the last line (return results;) and see the results as you type – Gavriel Jan 24 '16 at 22:29
  • Strange. What about the onQueryTextSubmit and onQueryTextChange functions? Do they get called at all when you start to type a search query? – Gavriel Jan 24 '16 at 23:27
  • They do. Honestly to me it looks like query I type in is just not compatible to anything, like method is all messed up. Probably gonna delete it and try something else. – user3576654 Jan 24 '16 at 23:44
  • You code looks just like mine, and here it works. You said that you never got to the 1st line of performFiltering. Even if you were searching for anything ("not compatible") it should go there. – Gavriel Jan 24 '16 at 23:46
  • My bad I didn't express well, when I meant nothing happens I meant that searching is not working, but it does get there..but seems like return results is not reacting. – user3576654 Jan 25 '16 at 00:07
  • Then try to debug it. Before you type anything you should see the whole list. When you type some gibrish, you should see less, and probably even nothing. – Gavriel Jan 25 '16 at 00:12
  • I do see the whole list, but when I try to type something like first letter of the name of the book it doesn't filter or do any kind of search.. – user3576654 Jan 25 '16 at 00:20
  • And when you search for gibrish? "Hydgkybfdhkourd"? – Gavriel Jan 25 '16 at 00:23
  • OMG! I'm so stupid! There's a bug in the emulator. It won't work there! You'll need to test this on a real device!!! – Gavriel Jan 25 '16 at 00:25
  • Kidding? what kind of bug? – user3576654 Jan 25 '16 at 00:32
  • You see, that means the filtering works. You just have to know what to search for – Gavriel Jan 25 '16 at 00:32
  • Doesn't matter, if gibrish filtered your list to empty it means it works – Gavriel Jan 25 '16 at 00:34
  • Great! I'll try to modify it and let you know the result – user3576654 Jan 25 '16 at 11:43
  • 1
    Thanks for the advice and your help – user3576654 Jan 25 '16 at 13:28