0

premise: i'm newbie on Java and Android, and i've search a lot for this but i don't understand how implement getFilter in my code.

this is MainActivity (relevant code):

public void loadList() {

  /* allProd and allSpec are ArrayList<String> */
  String[] allProdString = allProd.toArray(new String[allProd.size()]);
  String[] allSpecString = allSpec.toArray(new String[allSpec.size()]);

  listViewAdapter = new ListViewAdapter(this, allProdString, allSpecString);

  listView.setAdapter(listViewAdapter);

}

this is customAdapter:

public class ListViewAdapter extends BaseAdapter {

Activity context;
String title[];
String description[];

public ListViewAdapter(Activity context, String[] title, String[] description) {

    super();
    this.context = context;
    this.title = title;
    this.description = description;
}

public int getCount() {
    return title.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

private class ViewHolder {

    TextView txtViewTitle;
    TextView txtViewDescription;

}

public View getView(int position, View convertView, ViewGroup parent) {

    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "font/Simple_Print.ttf");

    ViewHolder holder;
    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.tabella_prodotti, null);
        holder = new ViewHolder();
        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.titoloProd);
        holder.txtViewDescription = (TextView) convertView.findViewById(R.id.subtitoloProd);

        holder.txtViewDescription.setTypeface(typeface);
        holder.txtViewTitle.setTypeface(typeface);

        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtViewTitle.setText(title [position]);
    holder.txtViewDescription.setText(description [position]);

    return convertView;

 }

}

how can I implement this function to have the ability to search within an array, and show the filtered results in the listview?

if you need any other info just ask! thanks

Ilario
  • 5,979
  • 2
  • 32
  • 46
  • implement `Filterable` interface. override getFilter and filter the data – Raghunandan Jan 16 '14 at 14:13
  • @Raghunandan ok.. can you show me an example please? – Ilario Jan 16 '14 at 14:13
  • have 2 sets of data display the filtered one on search and display the original data when there is no search. you can refer this http://stackoverflow.com/questions/13090046/how-to-implement-search-in-customlistview-based-on-class-item-of-pojo-class-in-a – Raghunandan Jan 16 '14 at 14:14
  • @Raghunandan so i should create other ArrayList and fill it with filtered data.. but this new ArrayList where should i create? in MainActivity or in CustomAdapter? because now a fill the original ArrayList with parse xml in MainActivity – Ilario Jan 16 '14 at 14:17
  • its just an idea you can decide how you want to implement your custom filter – Raghunandan Jan 16 '14 at 14:19
  • @Raghunandan i understand how i set two array in customAdapter, but i don't understand how pass filterable result.. i'm little confuse – Ilario Jan 16 '14 at 14:21
  • search on stackoverflow there are many examples http://stackoverflow.com/questions/20355177/how-to-add-search-functionality-in-listview and this http://stackoverflow.com/questions/17720481/how-could-i-filter-the-listview-using-baseadapter – Raghunandan Jan 16 '14 at 14:23
  • @Ilario instead of reinventing the wheel usw one of existing Filterable Adapters – pskink Jan 16 '14 at 14:25
  • @pskink but I do not want to reinvent anything! :( i find a tutorial for create a custom adapter and show listview.. but now i need to create a search in this list, and I'm stopping here, I could also change everything but do not know where to start – Ilario Jan 16 '14 at 14:27
  • @Ilario ok so start from here, use any Adapter except of BaseAdapter, they are Filterable, and use textFilterEnabled in your ListView, dont follow that stupid tutorials talking of EditTexts, TextWatchers etc – pskink Jan 16 '14 at 14:59
  • @pskink thanks for your suggestion, i really appreciate! ok then i will try other solutions – Ilario Jan 16 '14 at 15:05

2 Answers2

1

I think that your adapter should look like this. Also I would not recommend to instantiate objects inside getView() i.e typeface object

 public class ListViewAdapter extends BaseAdapter {
    String title[];
    String description[];
    ArrayList<String> filteredTitleList;
    ArrayList<String> filteredDescripionList;
    Typeface typeface;
    LayoutInflater inflater;

    public ListViewAdapter(Activity context, String[] title, String[] description) {
        super();
        this.context = context;
        this.title = title;
        this.description = description;

        typeface = Typeface.createFromAsset(context.getAssets(),"font/Simple_Print.ttf");

        inflater = context.getLayoutInflater();

        this.filteredTitleList = new ArrayList<String>();
        this.filteredDescripionList = new ArrayList<String>();
        applyFilter(null);
    }

    public void applyFilter(String filter){
        filteredTitleList.clear();
        filteredDescripionList.clear();

        for(int i=0; i < this.title.length; i++){
            String tempTitle = title[i];
            String tempDesc = description[i];

            if(filter == null || filter.equals("")){
                this.filteredTitleList.add(tempTitle);
                this.filteredDescripionList.add(tempDesc);
            }
            else if(tempTitle.toLowerCase().contains(filter.toLowerCase()) || tempDesc.toLowerCase().contains(filter.toLowerCase())){
                this.filteredTitleList.add(tempTitle);
                this.filteredDescripionList.add(tempDesc);
            }

        }
        this.notifyDataSetChanged();
    }

    public int getCount() {
        return filteredTitleList.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {

        TextView txtViewTitle;
        TextView txtViewDescription;

    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.tabella_prodotti, null);
            holder = new ViewHolder();
            holder.txtViewTitle = (TextView) convertView.findViewById(R.id.titoloProd);
            holder.txtViewDescription = (TextView) convertView.findViewById(R.id.subtitoloProd);

            holder.txtViewDescription.setTypeface(typeface);
            holder.txtViewTitle.setTypeface(typeface);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtViewTitle.setText(this.filteredTitleList.get(position));
        holder.txtViewDescription.setText(this.filteredDescriptionList.get(position));

        return convertView;

     }

    }
Corje
  • 158
  • 7
0

I have found a solution and share with you, I added addTextChangedListener to inputSearch in MainActivity:

inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            textLength = inputSearch.getText().length();
            //allProd_sort and allSpec_sort are ArrayList for search
            allProd_sort.clear();
            allSpec_sort.clear();
            String text = inputSearch.getText().toString();

            //allProdString is the String get from ArrayList allProd
            for (int y =0; y<allProdString.length; y++) {

                  //in my case the search works only if there are min 3 characters in search
                 if (textLength <= allProdString[y].length() && textLength >=3) {

                      if (Pattern.compile(Pattern.quote(text), Pattern.CASE_INSENSITIVE)
                                .matcher(allProdString[y]).find()) {

                          allProd_sort.add(allProdString[y]);
                          allSpec_sort.add(allSpecString[y]);
                      }
                  }
               }

               String[] allProdStringSort = allProd_sort.toArray(new String[allProd_sort.size()]);
               String[] allSpecStringSort = allSpec_sort.toArray(new String[allSpec_sort.size()]);

               listView.setAdapter(new ListViewAdapter(MainActivity.this, allProdStringSort, allSpecStringSort));
            }

        @Override
        public void afterTextChanged(Editable editable) {

      }
  });
Ilario
  • 5,979
  • 2
  • 32
  • 46