I am trying to use AutoCompleteTextView
for filtering and I'm having a problem with the filter. It returns all the Items in the ArrayList
instead of the filtered ones. Below is my code:
Filter nameFilter = new Filter() {
@Override
public String convertResultToString(Object resultValue) {
String str = ((State)(resultValue)).getName();
Log.e("Conv"+TAG, str);
return str;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if(constraint != null) {
suggestions.clear();
for (State customer : itemsAll) {
Log.e("Ite "+TAG, "" + itemsAll.size());
Log.e("Cons " + TAG, constraint.toString());
Log.e("Sta" + TAG, customer.getName()); //This is always null
if(customer.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
suggestions.add(customer);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
List<State> filteredList = (ArrayList<State>) results.values;
if(results != null && results.count > 0) {
clear();
Log.e("D "+TAG, ""+results.count);
for (State c : filteredList) {
Log.e("The "+TAG, c.getName() + " " + c.getId());
add(c);
notifyDataSetChanged();
}
} else {
Log.e(TAG, "Empty Filter");
notifyDataSetChanged();
}
}
};
And then I started logging around and I noticed this line is always null Log.e("Sta" + TAG, customer.getName()); //This is always null
Log.e("Sta" + TAG, customer.getName()); //This is always null
while this line Log.e("Cons " + TAG, constraint.toString());
and this line Log.e("Ite "+TAG, "" + itemsAll.size());
is never null. Constraint can't be null for sure unless no text was passed. But, for first object to be null when itemsAll.size()
is not null or empty. I am confused. And below, is my List
initialization
private static final String TAG = "StateAdapter";
private ArrayList<State> items;
private ArrayList<State> itemsAll;
private ArrayList<State> suggestions;
private Context context;
private int viewResourceId;
public StateAutoCompleteAdapter(Context context, int resource, int textViewResourceId, ArrayList<State> objects) {
super(context, resource, textViewResourceId, objects);
this.context = context;
this.items = objects;
this.itemsAll = new ArrayList<State>(items);
this.suggestions = new ArrayList<State>();
this.viewResourceId = resource;
}
And for the getView
function
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(viewResourceId, null);
}
State customer = items.get(position);
if (customer != null) {
TextView customerNameLabel = (TextView) v.findViewById(R.id.bakerName);
TextView bakerAddress = (TextView) v.findViewById(R.id.bakerAddress);
if (customerNameLabel != null) {
// Log.i(MY_DEBUG_TAG, "getView Customer Name:"+customer.getName());
customerNameLabel.setText(customer.getName());
bakerAddress.setVisibility(View.GONE);
}
}
return v;
}
So, I fixed the problem myself doing this. So, I use a normal BaseAdapter
and extend Filterable
So, for those that may need how to use RealmDB
with AutoCompleteTextView
there you go
public class LgasAutoCompleteAdapter extends BaseAdapter implements Filterable {
private static final String TAG = "LgasAutoComp";
private Context mContext;
private List<Lga> mResult = new ArrayList<>();
private LayoutInflater inflater;
public LgasAutoCompleteAdapter(Context mContext) {
this.mContext = mContext;
}
@Override
public int getCount() {
return mResult.size();
}
@Override
public Object getItem(int position) {
return mResult.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null)
view = inflater.inflate(R.layout.item_baker_autocomplete, parent, false);
Lga lga = mResult.get(position);
TextView customerNameLabel = (TextView) view.findViewById(R.id.bakerName);
TextView bakerAddress = (TextView) view.findViewById(R.id.bakerAddress);
if (lga != null) {
if (customerNameLabel != null) {
// Log.i(MY_DEBUG_TAG, "getView Customer Name:"+customer.getName());
customerNameLabel.setText(lga.getName());
bakerAddress.setVisibility(View.GONE);
}
}
return view;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
return null;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
if (constraint != null) {
//String query = constraint.toString().toLowerCase();
mResult = filterStates(constraint.toString());
Log.e(TAG, ""+mResult.size());
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
}
@NonNull
private List<Lga> filterStates(String query) {
Realm mRealm = RealmUtils.getRealmInstance(mContext);
return mRealm.where(Lga.class)
.contains("name", query)
.or()
.beginsWith("name", query)
.findAll();
}
}
clear() method might be causing problem. So please check its code for which array list you are cleaning.
Another problem is because you are using State customer = items.get(position);
in getView()
and using suggestions.add(customer);
in performFiltering(CharSequence constraint)
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.