I'm developing an Android app, and I'm having issues with the search function, because it doesn't find all the values that I enter. It seems to find only certain strings, instead of everything populated within the list. Like, for example, I'm able to find AB Product name, and TA Product name, but not Product name.
I'm suspecting an issue within the for loop code.
package com.example.user.sortiment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class SortimentAdapter extends BaseAdapter {
LayoutInflater mInflator;
List<Sortiment> map;
List<Sortiment> filterMap;
public void performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
if (Objects.equals(filterString, "")) {
filterMap = map;
notifyDataSetChanged();
return;
}
int count = map.size();
filterMap = new ArrayList<Sortiment>(count);
Sortiment filterableSortiment ;
for (int i = 0; i < count; i++) {
filterableSortiment = map.get(i);
if
(filterableSortiment.name.toLowerCase().contains(filterString)) {
filterMap.add(filterableSortiment);
}
}
notifyDataSetChanged();
}
public SortimentAdapter(Context c, List<Sortiment> inputMap) {
mInflator = (LayoutInflater)
c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
map = inputMap;
filterMap = inputMap;
}
@Override
public int getCount() {
return filterMap.size();
}
@Override
public Object getItem(int position) {
return filterMap.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = mInflator.inflate(R.layout.item_layout,null);
TextView nameTextView = (TextView) v.findViewById(R.id.nameTextView);
TextView priceTextView = (TextView) v.findViewById(R.id.priceTextView);
Sortiment viewObject = filterMap.get(position);
nameTextView.setText(viewObject.name);
priceTextView.setText(viewObject.ean.toString());
return v;
}
What I have tried:
I've tried messing with the loop a little bit, but I'm still a beginner among Java.