Fix SearchView not updating ListView in search activity

This commit is contained in:
Tanguy Herbron 2018-06-01 19:39:55 +02:00
parent b935cfa89f
commit 79484abb83
4 changed files with 67 additions and 58 deletions

View File

@ -147,16 +147,9 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear
@Override @Override
public boolean onQueryTextChange(String text) public boolean onQueryTextChange(String text)
{ {
filter.filter(text); adapter.getFilter().filter(text);
if (TextUtils.isEmpty(text)) { return false;
listView.clearTextFilter();
} else {
Log.d("moodl", "Set filter : " + text);
listView.setFilterText(text);
}
return true;
} }
@Override @Override
@ -167,13 +160,11 @@ public class CurrencySelectionActivity extends AppCompatActivity implements Sear
private void detailsEvent() private void detailsEvent()
{ {
setupAdapter();
setupList();
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
setupAdapter();
setupList();
setupSearchView(); setupSearchView();
expand(findViewById(R.id.listContainerLayout)); expand(findViewById(R.id.listContainerLayout));

View File

@ -16,6 +16,7 @@ import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout; import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.graphics.Palette; import android.support.v7.graphics.Palette;
import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;

View File

@ -214,7 +214,7 @@ public class CurrencyCardview extends CardView {
setClickable(false); setClickable(false);
setFocusable(false); setFocusable(false);
setCardBackgroundColor(MoodlBox.getColor(R.id.withText, getContext())); setCardBackgroundColor(MoodlBox.getColor(R.color.white, getContext()));
} }
private void setupLineChart(final Currency currency) private void setupLineChart(final Currency currency)

View File

@ -7,7 +7,9 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter; import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView; import android.widget.TextView;
import com.nauk.moodl.DataManagers.CurrencyData.Currency; import com.nauk.moodl.DataManagers.CurrencyData.Currency;
@ -19,38 +21,52 @@ import java.util.ArrayList;
* Created by Guitoune on 17/01/2018. * Created by Guitoune on 17/01/2018.
*/ */
public class CurrencyListAdapter extends ArrayAdapter<Currency> { public class CurrencyListAdapter extends BaseAdapter implements Filterable {
private ArrayList<Currency> tempCurrency, suggestions; private ArrayList<Currency> currencies, suggestions;
private Context context; private Context context;
private CustomFilter filter;
public CurrencyListAdapter(Context context, ArrayList<Currency> currencies) { public CurrencyListAdapter(Context context, ArrayList<Currency> 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.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 @NonNull
@Override @Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) 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) { 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 currencyName = convertView.findViewById(R.id.currencyName);
TextView currencySymbol = convertView.findViewById(R.id.currencySymbol); TextView currencySymbol = convertView.findViewById(R.id.currencySymbol);
if (currencyName != null) if (currencyName != null)
currencyName.setText(currency.getName()); currencyName.setText(currencies.get(position).getName());
if(currencySymbol != null) if(currencySymbol != null)
{ {
currencySymbol.setText(currency.getSymbol()); currencySymbol.setText(currencies.get(position).getSymbol());
} }
if (position % 2 == 0) if (position % 2 == 0)
@ -64,56 +80,57 @@ public class CurrencyListAdapter extends ArrayAdapter<Currency> {
@NonNull @NonNull
@Override @Override
public Filter getFilter() { public Filter getFilter() {
return myFilter;
if(filter == null)
{
filter = new CustomFilter();
}
return filter;
} }
private Filter myFilter = new Filter() { class CustomFilter extends Filter
@Override {
public CharSequence convertResultToString(Object resultValue) {
Currency currency = (Currency) resultValue;
return currency.getName();
}
@Override @Override
protected FilterResults performFiltering(CharSequence constraint) { protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) { FilterResults results = new FilterResults();
suggestions.clear();
int i = 0; if(constraint != null && constraint.length() > 0)
String temp = constraint.toString().toLowerCase(); {
constraint = constraint.toString().toLowerCase();
while(i < tempCurrency.size()) ArrayList<Currency> filters = new ArrayList<Currency>();
for(int i = 0; i < suggestions.size(); i++)
{ {
Currency currency = tempCurrency.get(i); if(suggestions.get(i).getName().toLowerCase().contains(constraint) || suggestions.get(i).getSymbol().toLowerCase().contains(constraint))
if (currency.getName().toLowerCase().startsWith(temp) {
|| currency.getSymbol().toLowerCase().startsWith(temp)) { Currency currency = new Currency(suggestions.get(i).getName(), suggestions.get(i).getSymbol());
suggestions.add(currency);
filters.add(currency);
} }
i++;
} }
FilterResults filterResults = new FilterResults(); results.count = filters.size();
filterResults.values = suggestions; results.values = filters;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
} }
else
{
results.count = suggestions.size();
results.values = suggestions;
}
return results;
} }
@Override @Override
protected void publishResults(CharSequence constraint, FilterResults results) { protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<Currency> c = (ArrayList<Currency>) results.values;
Log.d("moodl", "Q " + constraint); if(results != null)
{
if (results != null && results.count > 0) { currencies = (ArrayList<Currency>) results.values;
clear();
for (Currency currency : c) {
add(currency);
notifyDataSetChanged();
}
} }
notifyDataSetChanged();
} }
}; }
} }