Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

/**
 * 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;
}


What I have tried:

I've tried messing with the loop a little bit, but I'm still a beginner among Java.
Posted
Updated 10-Aug-18 0:03am
v2
Comments
David Crow 10-Aug-18 9:55am    
Have you stepped through the code to find out exactly what is happening? I suggest you do that first, before asking for help. That way, you've gone through the process of figuring out things like how many iterations the loop made, what are the values of variables such as constraint, filterString, count, i, and filterMap, and what pattern of search strings cause the code to fail/work.
Mike V Baker 10-Aug-18 10:29am    
You could use for (Sortiment filterableSortiment : map) rather than using i as iterator. But I don't think that would change your results, just a different way to iterate. Have you written the contents of your vars out to a log so you can verify that what you think you're looking for is there? My first debugging technique - verify the data. Dump the contents of map out to the console prior to running the loop, and dump the contents of filterableSortiment as it is being tested.
p.s. don't pre-init the filterMap to the same size as map. You'll never need that many unless all items match. Just create it empty, 'add' will expand it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900