Android : AutoCompleteTextView not working after backspace

on Monday, March 30, 2015


I have been searching the web the whole damn day on this, hope some of you can help me.


Currently my AutoCompleteTextView works as long as i don't press backspace.


Adapter



public class IngredientAutoCompleteAdapter extends ArrayAdapter<Ingredient> implements Filterable{
private static final String TAG = "IngredientAutoCompleteAdapter";
private Context context;
private List<Ingredient> ingredients;
private DomainFacade service;
private Filter filter;

public IngredientAutoCompleteAdapter(Context context, int resource, List<Ingredient> objects) {
super(context, resource, objects);
this.context = context;
ingredients = objects;
filter = new IngredientNameFilter(ingredients);
try {
service = DomainFacade.getinstance();
} catch (ServiceException e) {
e.printStackTrace();
}
}

@Override
public Ingredient getItem(int position) {
return ingredients.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public Filter getFilter() {
return filter;
}

@Override
public View getView(int pos, View convertView, ViewGroup parent) {
//get inflater to inflate the listview with its rows
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//init View that holds the entire row
View rowView = inflater.inflate(R.layout.autocomplete_row, parent, false);
//init TextViews
TextView nameTXTV = (TextView) rowView.findViewById(R.id.autocomplete_row_txtv_name);
TextView calTXTV = (TextView) rowView.findViewById(R.id.autocomplete_row_txtv_cal);
TextView fatTXTV = (TextView) rowView.findViewById(R.id.autocomplete_row_txtv_fat);
TextView purchaseTXTV = (TextView) rowView.findViewById(R.id.autocomplete_row_txtv_lastpurchase);
//Set font styles
FontLoader.GRANDHOTEL_REGULAR.apply(context, nameTXTV);
FontLoader.ELLIPTICA.apply(context, calTXTV);
FontLoader.ELLIPTICA.apply(context, fatTXTV);
FontLoader.ELLIPTICA.apply(context, purchaseTXTV);
//set text
Logger.log(TAG, ingredients.toString());
Logger.log(TAG, pos + " - " + ingredients.get(pos).getName());
nameTXTV.setText(ingredients.get(pos).getName());
calTXTV.setText("Cal: " + ingredients.get(pos).getCal());
fatTXTV.setText("Fat: " + ingredients.get(pos).getFat());
String date;
if (!service.getIngredientLastPurchaseDate(ingredients.get(pos).getName()).equals(new Date(0))) {
//format date nicely
date = DateFormat.getInstance().format(service.getIngredientLastPurchaseDate(ingredients.get(pos).getName()));
} else {
date = "never";
}
purchaseTXTV.setText("Last purchase: " + date);


return rowView;
}

//Filter
private class IngredientNameFilter extends Filter {
private static final String TAG = "IngredientNameFilter";
private final List<Ingredient> data;

public IngredientNameFilter(List<Ingredient> data){this.data=data;}

@Override
public String convertResultToString(Object resultValue) {
return ((Ingredient)resultValue).getName();
}

@Override
protected FilterResults performFiltering(CharSequence constraint) {

List<Ingredient> result = new ArrayList<>();
if(constraint != null) {
Logger.log(TAG,"constraint="+constraint.toString());
Logger.log(TAG,"ingredients="+data);
for (Ingredient ingredient : data) {
if(ingredient.getName().toLowerCase().contains(constraint.toString().toLowerCase())){
result.add(ingredient);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = result;
filterResults.count = result.size();
Logger.log(TAG,"filterResults="+filterResults.values);
return filterResults;
} else {
return new FilterResults();
}
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
Logger.log(TAG,"publishResult");
Logger.log(TAG,"constraint="+constraint);
Logger.log(TAG,"FilterResults="+results.values);

if (results != null && results.count > 0) {
// we have filtered results
addAll((ArrayList<Ingredient>) results.values);
} else {
// no filter, add entire original list back in
addAll(data);
}

notifyDataSetChanged();
}

}
}


Note that this is a seprate class, meaning it does not run within a UIThread.


Fragment



public static class NewIngredientFragment extends Fragment {
protected AutoCompleteTextView ingredientNameACTV;
protected EditText calED, fatED, qtyTV;
private DomainFacade service;

public static NewIngredientFragment getInstance() {
return new NewIngredientFragment();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_ingredient, container, false);
try {
service = DomainFacade.getinstance();
} catch (ServiceException e) {
e.printStackTrace();
}
initLayoutComponents(v);
// Inflate the layout for this fragment
return v;

}

private void initLayoutComponents(View view) {
ingredientNameACTV = (AutoCompleteTextView) view.findViewById(R.id.newIngredient_name_ACTV);
calED = (EditText) view.findViewById(R.id.newIngredient_cal_ET);
fatED = (EditText) view.findViewById(R.id.newIngredient_fat_ET);
qtyTV = (EditText) view.findViewById(R.id.newIngredient_qty_ET);
//List<String> ingredients = new LinkedList<>(service.getIngredients().keySet());
List<Ingredient> ingredients = new LinkedList<>(service.getIngredients().values());
Logger.log(TAG, ingredients.toString());
//IngredientAutoCompleteAdapter adapter = new IngredientAutoCompleteAdapter(view.getContext(), R.layout.autocomplete_row, ingredients);
ingredientNameACTV.setAdapter(new IngredientAutoCompleteAdapter(view.getContext(),R.layout.autocomplete_row, ingredients));
ingredientNameACTV.setThreshold(1);
}


}


Note, this Fragment class is an inner class within a FragmentPagerAdapter, which then again is a inner class within a ActionBarActivity


0 comments:

Post a Comment