I have a ListView with an EditText arriva that is used to filter the elements in the ListView, only when entering a character, everything is deleted from the ListView and when I erase what I have in the EditText everything is shown again in the ListView, this is the code I have so far where I have the ListView full
public void populatingListView()
{
try{
AndroidDatabase myDB = new AndroidDatabase(contx);
myDB.Open();
productList = myDB.getProduct();
Log.i(TAG, productList.toString());
myDB.close();
Log.i(TAG, productList.toString());
adapterProducts = new ProductAdapter(this.contx,productList);
inventoryListView.setAdapter(adapterProducts);
}
catch(Exception e){
Log.i(TAG,"Error de llenado listview: "+ e.getMessage());
}
}
where I do the search
searchBox.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
if (s.length() >= SEARCH_LIMIT) {
search();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
Here is the method I use above
private void search() {
String search = searchBox.getText().toString();
if (search.equals("/demo")) {
testAddProduct();
searchBox.setText("");
} else if (search.equals("/clear")) {
DatabaseExecutor.getInstance().dropAllData();
searchBox.setText("");
}
else if (search.equals("")) {
populatingListView(productCatalog.getAllProduct());
} else {
List<Product> result = productCatalog.searchProduct(search);
populatingListView(result);
if (result.isEmpty()) {
}
}
}