0

Im new to Android Development please help me. These past few days I've been working on application that involves searching of recipe by ingredient. So Ive created an interface that lets the user add an ingredient and the app suggests as the user types.

For many hours of searching here and there, Ive got the AutoCompleteTextView working perfectly, well at least for only one String(the name).

But my problem is, my customAdapter has TWO strings(name and assoc) that I want my suggestions to be compared into.

Here is my model class for my Ingredient Object:

public class Ingredient {

 private String code = null;
 private String name = null;
 private String assoc = null;

public Ingredient(String code, String name, String assoc ) {
    super();
    this.code = code;
    this.name = name;
    this.assoc = assoc;
}

public String getCode() {
    return code;
}
public void setCode(String code) {
    this.code = code;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public void setAssoc(String assoc) {
    this.assoc = assoc;
       }

public String getAssoc() {
   return  this.assoc;
}

}

And here is my arrayAdapter for the AutoCompleteTextView

 public class CustomIngSearchAdapter extends ArrayAdapter<Ingredient> {

private ArrayList<Ingredient> itemsAll;
private ArrayList<Ingredient> suggestions;

public CustomIngSearchAdapter(Context context, int viewResourceId,
                            ArrayList<Ingredient> items) {
    super(context, viewResourceId, items);
    this.itemsAll = items;
    this.suggestions = new ArrayList<Ingredient>();
    this.viewResourceId = viewResourceId;
}

   .
   .//getView Method
   .


  @Override
  public Filter getFilter() {
    return new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                suggestions.clear();

                for (Ingredient ingredient : itemsAll) {
                    if (ingredient.getName().toLowerCase()
                            .contains(constraint.toString().toLowerCase()) ) {                                suggestions.add(ingredient);
                    }
                }
                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) {
            @SuppressWarnings("unchecked")
            ArrayList<Ingredient> filteredList = (ArrayList<Ingredient>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (Ingredient c : filteredList) {
                    add(c);
                }
                notifyDataSetChanged();
            }
        }


    };
}

What I've tried already..

      //adding OR
for (Ingredient ingredient : itemsAll) {
      if (ingredient.getName().toLowerCase()
           .contains(constraint.toString().toLowerCase())  ||                                
           ingredient.getAssoc().toLowerCase()
      .contains(constraint.toString().toLowerCase())) {

                        suggestions.add(ingredient);
                    }
                }

But when I do this, the suggestions are not displaying..

Again, the arrayAdapter class is working fine for the name only

PS: I also tried making two separate FOR LOOPS...still unlucky

PS 2: please bare with my bad English . :)

jamsubzero
  • 31
  • 4

0 Answers0