I'm using Java to build an Android application, and I'm trying to build a search function to find a result from a listview. So far, I've managed to get my search function to work, but it doesn't work properly - it only manages to find certain criteria. For example, it finds TC Product name, JC Product name but doesn't find Product name. And if I try to find certain string values, it doesn't seem to find all values either.
Here's my 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;
/**
* Created by user on 11.05.2018
*/
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(String.format("%.0f", prices.get(position)));
priceTextView.setText(viewObject.ean.toString());
return v;
}
}
I'm suspecting an issue in: if (filterableSortiment.name.toLowerCase().contains(filterString)) { , but im not entirely sure.
Any ideas? Thanks