Не работает фильтрация
Делаю фильтрацию по listview, значения в него помещаю из api. Когда что-то ввожу в поиск listview очищается, если стереть, то listview остаётся пустым. Что здесь не так?
Каталог:
public class Catalog extends AppCompatActivity {
private ProductAdapter mAdapter;
private List<Product> mProductList = new ArrayList<>();
private ListView lvProduct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setSelectedItemId(R.id.navigation_catalog);
bottomNavigationView.setOnItemSelectedListener(menuItem -> {
if(menuItem.getItemId() == R.id.navigation_search) {
startActivity(new Intent(getApplicationContext(),MainActivity.class));
overridePendingTransition(0,0);
return true;
}
if(menuItem.getItemId() == R.id.navigation_order) {
startActivity(new Intent(getApplicationContext(), Order.class));
overridePendingTransition(0, 0);
return true;
}
if(menuItem.getItemId() == R.id.navigation_catalog){
return true;
}
return false;
});
lvProduct = findViewById(R.id.listView);
mAdapter = new ProductAdapter(Catalog.this, mProductList);
lvProduct.setAdapter(mAdapter);
new GetProducts().execute();
}
private class GetProducts extends AsyncTask<Void,Void,String> {
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL("http://192.168.0.102:3000/api/Product");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder result = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONArray tempArray = new JSONArray(s);
for (int i = 0; i < tempArray.length(); i++) {
JSONObject productJson = tempArray.getJSONObject(i);
Product tempProduct = new Product(
productJson.getInt("Код"),
productJson.getString("Наименование"),
productJson.getString("Штрих_код"),
productJson.getString("Изображение"),
productJson.getString("Артикул"),
productJson.getString("Цена"),
productJson.getString("Стеллаж"),
productJson.getString("Проём")
);
mProductList.add(tempProduct);
mAdapter.notifyDataSetChanged();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem menuItem = menu.findItem(R.id.actionSearch);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText))
{
mAdapter.Filter("");
lvProduct.clearTextFilter();
}
else
{
mAdapter.Filter(newText);
}
return true;
}
});
return true;
}
}
Адаптер с фильтрацией (сократил):
private Context mcontext;
private List<Product> mProductList;
LayoutInflater inflater;
ArrayList<Product> arrayList;
public ProductAdapter(Context context, List<Product> mProductList) {
this.mcontext = context;
this.mProductList = mProductList;
inflater = LayoutInflater.from(mcontext);
this.arrayList = new ArrayList<Product>();
this.arrayList.addAll(mProductList);
}
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = View.inflate(mcontext, R.layout.item_product, null);
ImageView imageView = v.findViewById(R.id.imageViewItem);
TextView textName = v.findViewById(R.id.textNameItem);
TextView textBarcode = v.findViewById(R.id.textViewBarcodeItem);
TextView textPrice = v.findViewById(R.id.textPriceItem);
Product currentProduct = mProductList.get(i);
imageView.setImageBitmap(currentProduct.getBitmapSource());
textName.setText(currentProduct.getName());
textBarcode.setText(currentProduct.getBarcode());
textPrice.setText(currentProduct.getPrice());
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentDetails = new Intent(mcontext, Details.class);
intentDetails.putExtra("product", currentProduct);
mcontext.startActivity(intentDetails);
}
});
return v;
}
public void Filter(String charText){
charText = charText.toLowerCase(Locale.getDefault());
mProductList.clear();
if (charText.length() == 0)
{
mProductList.addAll(arrayList);
}
else
{
for (Product product : arrayList)
{
if(product.getName().toLowerCase(Locale.getDefault()).contains(charText))
{
mProductList.add(product);
}
}
}
notifyDataSetChanged();
}
Меню:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/actionSearch"
android:title="Поиск"
android:icon="@drawable/baseline_search_24"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView" />