How to create a custom filter in the android lestview that searches for words after the space character

Asked

Viewed 209 times

1

In this code the filter is working until the first word. From the moment I enter the space character the filter stops working. Example: Search "Steve Jobs", the system gets lost. Now if you search only until "Steve" it returns right.

ArrayList<> dados = new ArrayList<>();

SQLiteDatabase db1 = base.getReadableDatabase();

  Cursor c = db1.rawQuery("SELECT * FROM produtos WHERE pcodgrupo LIKE '2' ORDER BY pcodprod ",null);

   while (c.moveToNext())
      {
          HashMap map = new HashMap();
          map.put("pcodprod", c.getString(0));
          map.put("pdescricao", c.getString(2));
          map.put("compl", c.getString(5));
          map.put("pprecovenda", c.getString(9));
          map.put("plocalizacaoprod", c.getString(25));
          map.put("pprecoprazo", c.getString(c.getColumnIndex("pprecoprazo")));
          dados.add(map);
      }

String[] from = new String[]{"pcodprod", "plocalizacaoprod", "pdescricao", "compl", "pprecovenda","pprecoprazo"};

 int layout = R.layout.list_teste_item2;

 int[] to = new int[]{R.id.id, R.id.localP, R.id.nome, R.id.comple, R.id.prevoAv, R.id.precoPrazo};

ListView lv = (ListView) findViewById(R.id.listView2);

adapter = new SimpleAdapter(this, dados, layout, from, to);
lv.setAdapter(adapter);


 edtPesquisa.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) {
     adapter.getFilter().filter(s.toString().trim());
 }
 @Override
 public void afterTextChanged(Editable s) {

 }

1 answer

0

The SimpleAdapter does not support some filtors, switch to a ArrayAdapter or use a customized adapter, this for example implements Filterable, then allows a good amount of filters.

  • Thanks Lucas for the tips. I wouldn’t happen to have any example of Filterable because with Arrayadapter I tried and error in the code. Below is my code: adapter1 = new Arrayadapter <Arraylist<Hashmap<String, String>>>(this, layout, to, data); Lv.setAdapter(adapter1);

  • @xvesdeveloper on the link I left has a custom Array that implements Filterable, it has MIT license, so I think you can add it as a dependency on your project and use it. Or open it and see how it’s done

Browser other questions tagged

You are not signed in. Login or sign up in order to post.