From 79484abb8315a4626dd9be3360698a7f6e144912 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Fri, 1 Jun 2018 19:39:55 +0200 Subject: [PATCH] Fix SearchView not updating ListView in search activity --- .../Activities/CurrencySelectionActivity.java | 17 +-- .../HomeActivityFragments/Summary.java | 1 + .../CurrencyData/CurrencyCardview.java | 2 +- .../LayoutManagers/CurrencyListAdapter.java | 105 ++++++++++-------- 4 files changed, 67 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/com/nauk/moodl/Activities/CurrencySelectionActivity.java b/app/src/main/java/com/nauk/moodl/Activities/CurrencySelectionActivity.java index b8ea34b..ffe0b52 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/CurrencySelectionActivity.java +++ b/app/src/main/java/com/nauk/moodl/Activities/CurrencySelectionActivity.java @@ -147,16 +147,9 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear @Override public boolean onQueryTextChange(String text) { - filter.filter(text); + adapter.getFilter().filter(text); - if (TextUtils.isEmpty(text)) { - listView.clearTextFilter(); - } else { - Log.d("moodl", "Set filter : " + text); - listView.setFilterText(text); - } - - return true; + return false; } @Override @@ -167,13 +160,11 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear private void detailsEvent() { - setupAdapter(); - - setupList(); - runOnUiThread(new Runnable() { @Override public void run() { + setupAdapter(); + setupList(); setupSearchView(); expand(findViewById(R.id.listContainerLayout)); diff --git a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java index 10727b7..a4e0ea8 100644 --- a/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java +++ b/app/src/main/java/com/nauk/moodl/Activities/HomeActivityFragments/Summary.java @@ -16,6 +16,7 @@ import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.graphics.Palette; +import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; diff --git a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyCardview.java b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyCardview.java index a3a29ca..7ba63b9 100644 --- a/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyCardview.java +++ b/app/src/main/java/com/nauk/moodl/DataManagers/CurrencyData/CurrencyCardview.java @@ -214,7 +214,7 @@ public class CurrencyCardview extends CardView { setClickable(false); setFocusable(false); - setCardBackgroundColor(MoodlBox.getColor(R.id.withText, getContext())); + setCardBackgroundColor(MoodlBox.getColor(R.color.white, getContext())); } private void setupLineChart(final Currency currency) diff --git a/app/src/main/java/com/nauk/moodl/LayoutManagers/CurrencyListAdapter.java b/app/src/main/java/com/nauk/moodl/LayoutManagers/CurrencyListAdapter.java index bd2ba97..802bd68 100644 --- a/app/src/main/java/com/nauk/moodl/LayoutManagers/CurrencyListAdapter.java +++ b/app/src/main/java/com/nauk/moodl/LayoutManagers/CurrencyListAdapter.java @@ -7,7 +7,9 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.BaseAdapter; import android.widget.Filter; +import android.widget.Filterable; import android.widget.TextView; import com.nauk.moodl.DataManagers.CurrencyData.Currency; @@ -19,38 +21,52 @@ import java.util.ArrayList; * Created by Guitoune on 17/01/2018. */ -public class CurrencyListAdapter extends ArrayAdapter { +public class CurrencyListAdapter extends BaseAdapter implements Filterable { - private ArrayList tempCurrency, suggestions; + private ArrayList currencies, suggestions; private Context context; + private CustomFilter filter; public CurrencyListAdapter(Context context, ArrayList currencies) { - super(context, android.R.layout.simple_list_item_1, currencies); - this.tempCurrency = new ArrayList<>(currencies); - this.suggestions = new ArrayList<>(currencies); - this.context = context; + this.currencies = currencies; + this.suggestions = currencies; + } + + @Override + public int getCount() { + return currencies.size(); + } + + @Override + public Object getItem(int position) { + return currencies.get(position); + } + + @Override + public long getItemId(int position) { + return currencies.indexOf(getItem(position)); } @NonNull @Override public View getView(int position, View convertView, @NonNull ViewGroup parent) { - Currency currency = getItem(position); + LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) { - convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_currency_row, parent, false); + convertView = inflater.inflate(R.layout.custom_currency_row, parent, false); } TextView currencyName = convertView.findViewById(R.id.currencyName); TextView currencySymbol = convertView.findViewById(R.id.currencySymbol); if (currencyName != null) - currencyName.setText(currency.getName()); + currencyName.setText(currencies.get(position).getName()); if(currencySymbol != null) { - currencySymbol.setText(currency.getSymbol()); + currencySymbol.setText(currencies.get(position).getSymbol()); } if (position % 2 == 0) @@ -64,56 +80,57 @@ public class CurrencyListAdapter extends ArrayAdapter { @NonNull @Override public Filter getFilter() { - return myFilter; + + if(filter == null) + { + filter = new CustomFilter(); + } + return filter; } - private Filter myFilter = new Filter() { - @Override - public CharSequence convertResultToString(Object resultValue) { - Currency currency = (Currency) resultValue; - return currency.getName(); - } - + class CustomFilter extends Filter + { @Override protected FilterResults performFiltering(CharSequence constraint) { - if (constraint != null) { - suggestions.clear(); + FilterResults results = new FilterResults(); - int i = 0; - String temp = constraint.toString().toLowerCase(); + if(constraint != null && constraint.length() > 0) + { + constraint = constraint.toString().toLowerCase(); - while(i < tempCurrency.size()) + ArrayList filters = new ArrayList(); + + for(int i = 0; i < suggestions.size(); i++) { - Currency currency = tempCurrency.get(i); - if (currency.getName().toLowerCase().startsWith(temp) - || currency.getSymbol().toLowerCase().startsWith(temp)) { - suggestions.add(currency); + if(suggestions.get(i).getName().toLowerCase().contains(constraint) || suggestions.get(i).getSymbol().toLowerCase().contains(constraint)) + { + Currency currency = new Currency(suggestions.get(i).getName(), suggestions.get(i).getSymbol()); + + filters.add(currency); } - i++; } - FilterResults filterResults = new FilterResults(); - filterResults.values = suggestions; - filterResults.count = suggestions.size(); - return filterResults; - } else { - return new FilterResults(); + results.count = filters.size(); + results.values = filters; } + else + { + results.count = suggestions.size(); + results.values = suggestions; + } + + return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { - ArrayList c = (ArrayList) results.values; - Log.d("moodl", "Q " + constraint); - - if (results != null && results.count > 0) { - clear(); - for (Currency currency : c) { - add(currency); - notifyDataSetChanged(); - } + if(results != null) + { + currencies = (ArrayList) results.values; } + + notifyDataSetChanged(); } - }; + } }