package www.finefood.ExpansionEditText;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
import www.finefood.R;
public class MyArrayAdapter extends ArrayAdapter<MyForArrayAdapter> implements Filterable {
private ArrayList<MyForArrayAdapter> list = new ArrayList<>();
private ArrayList<MyForArrayAdapter> listCopy = new ArrayList<>();
private Activity context;
private Integer resource;
private LayoutInflater inflater;
public MyArrayAdapter(Context context, int resource,
ArrayList<MyForArrayAdapter> list) {
super(context, resource, list);
this.resource = resource;
this.context = (Activity) context;
this.list = list;
inflater = this.context.getLayoutInflater();
listCopy.addAll(list); //тут
}
class ViewHolder {
protected TextView text;
}
/**
* При получении совпадения со вводом создаёт view список и заполняет его данными
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
String str = list.get(position).getNameProduct();
ViewHolder viewHolder;
if (convertView == null) {
view = inflater.inflate(resource, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.text_view_array_adapter);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
if (viewHolder != null)
viewHolder.text.setText(str);
return view;
}
@Override
public int getCount() {
return list.size();
}
@Override
public MyForArrayAdapter getItem(int position) {
return list.get(position);
}
@NonNull
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults filterResults = new FilterResults();
if (charSequence != null) {
list.clear(); // тут
list.addAll(listCopy); // тут
ArrayList<MyForArrayAdapter> tempList = new ArrayList<>();
for (int i = 0; list.size() > i; ++i) {
MyForArrayAdapter mfad = list.get(i);
if (mfad.getNameProduct().toLowerCase().contains(charSequence))
tempList.add(mfad);
}
filterResults.count = tempList.size();
filterResults.values = tempList;
}
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
if (charSequence != null) {
ArrayList<MyForArrayAdapter> tempList = (ArrayList<MyForArrayAdapter>) filterResults.values;
clear();
for (int i = 0; tempList.size() > i; ++i)
add(tempList.get(i));
}
notifyDataSetChanged();
}
};
return filter;
}
}
public class MyArrayAdapter extends ArrayAdapter<MyForArrayAdapter>
//allMyForArrayAdapter = это ArrayList<String> - отдельно массив string productName - хотя мне кажется это костылём
private class MyFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
charSequence = charSequence.toString().toLowerCase();
FilterResults results = new FilterResults();
if (charSequence.toString().length() > 0) {
ArrayList<String> stringList = new ArrayList<>();
for (int i = 0; allMyForArrayAdapter.size() > i; ++i) {
String str = allMyForArrayAdapter.get(i);
if (str.toLowerCase().contains(charSequence))
stringList.add(str);
}
results.count = stringList.size();
results.values = stringList;
} else {
synchronized (this) {
results.values = allMyForArrayAdapter;
results.count = allMyForArrayAdapter.size();
}
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
/**а что тут написать?*/
}
}
@Override - не нужен, верно?