I'm writing periodic table software, and one section is a list of chemical formulas. The app allows the user to search from the list but i get some trouble in filtering the results
FurmulaProvider.java
public class FurmolaProvider {
private int id;
private String sympol;
private String name;
public FurmolaProvider(int id, String sym, String name) {
this.setId(id);
this.setName(name);
this.setSympol(sym);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSympol() {
return sympol;
}
public void setSympol(String sympol) {
this.sympol = sympol;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
And the Custom Adapter is :
public class FurmolaAdapter extends ArrayAdapter implements Filterable {
List list = new ArrayList<>();
List disp_ls = new ArrayList<>();
public FurmolaAdapter(Context context, int resource) {
super(context, resource);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
DataHandler handler;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.formula_item, parent, false);
handler = new DataHandler();
handler.molar = (TextView) row.findViewById(R.id.molar_mass_item);
handler.name = (TextView) row.findViewById(R.id.name_item);
handler.sympol = (TextView) row.findViewById(R.id.symp_item);
row.setTag(handler);
}else {
handler = new DataHandler();
handler.molar = (TextView) row.findViewById(R.id.molar_mass_item);
handler.name = (TextView) row.findViewById(R.id.name_item);
handler.sympol = (TextView) row.findViewById(R.id.symp_item);
row.setTag(handler);
}
FurmolaProvider provider ;
provider = (FurmolaProvider) this.getItem(position);
handler.molar.setText(provider.getMolar() + " ");
handler.molar.setBackground(getContext().getResources().getDrawable (R.drawable.molar_back));
handler.name.setText(provider.getName());
handler.sympol.setText(provider.getSympol());
return row;
}
private int getColor (double mole){
int res = 0;
if (mole > 250 && mole < 500) {
}
return android.R.color.holo_green_dark;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
disp_ls.add(object);
}
static class DataHandler {
TextView molar;
TextView sympol;
TextView name;
}
@Override
public int getCount() {
return this.list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return this.list.get(position);
}
@NonNull
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List filtList = new ArrayList<>();
if (list == null) {
list = new ArrayList(disp_ls);
}
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = disp_ls.size();
results.values = disp_ls;
} else {
constraint = constraint.toString().toLowerCase();
for(int i=0; i < disp_ls.size() ; i++) {
FurmolaProvider data = (FurmolaProvider) disp_ls.get(i);
String da = data.getSympol() + " " + data.getName() ;
if (da.toLowerCase().contains(constraint)) {
filtList.add(disp_ls.get(i));
}
}
results.count = filtList.size();
results.values = filtList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
list = (List) results.values;
notifyDataSetChanged();
}
};
return super.getFilter();
}
}
Note: disp_ls have all the elements
,
when i run this code, nothing happened. can you help ?! :)
You have to return the filter you created
return filter;
instead of
return super.getFilter();