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) {
/**а что тут написать?*/
}
}
public class StateArrayAdapter {
private Integer ID;
private String nameProduct;
public StateArrayAdapter(Integer ID, String nameProduct) {
this.ID = ID;
this.nameProduct = nameProduct;
}
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public String getNameProduct() {
return nameProduct;
}
public void setNameProduct(String nameProduct) {
this.nameProduct = nameProduct;
}
}
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import www.finefood.R;
import java.util.ArrayList;
import java.util.List;
public class SpecialArrayAdapter extends ArrayAdapter<StateArrayAdapter> {
private LayoutInflater inflater;
private int layout;
private List<StateArrayAdapter> list;
public SpecialArrayAdapter(Context context,
int resource, int textViewResourceId,
ArrayList<StateArrayAdapter> list) {
super(context, resource, textViewResourceId, list);
this.list = list;
this.layout = resource;
this.inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflater.inflate(this.layout, parent, false);
TextView textView = (TextView) view.findViewById(R.id.text_view_array_adapter);
StateArrayAdapter stateArrayAdapter = list.get(position);
textView.setText(stateArrayAdapter.getNameProduct());
return view;
}
}
private void addAdapter(Context context) {
this.setAdapter(new SpecialArrayAdapter(context,
R.layout.list_item_array_adpter,R.id.text_view_array_adapter,
GetSQLCursorProductName.getInstance().list));
}