Search filter Expandablelistview Header Items

You can post your problem related to Computer Software Projects here. We will try our best to help you out.
Post Reply
Vazquez
Posts: 31
Joined: Mon Mar 06, 2017 8:50 am

Search filter Expandablelistview Header Items

Post by Vazquez » Thu Mar 09, 2017 8:49 am

I'm about to create an expandableListview with implemented Searchview. My SearchView can filter child items and only the child items. I want to create a search filter that can also filter the header items in expandablelistview. how can I do that?

here's my code for the expandablelistview with search filter. here is my Adapter:

Code: Select all

package com.teamamazing.search;


import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;

import android.content.Context;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import android.view.View.OnClickListener;


public class Accomodation_Adapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<AccomodationHeaderRow> headerRows;
    private ArrayList<AccomodationHeaderRow> originalList;

    public Accomodation_Adapter(Context context, ArrayList<AccomodationHeaderRow> headerRows) {
        this.context = context;
        this.headerRows = new ArrayList<AccomodationHeaderRow>();
        this.headerRows.addAll(headerRows);
        this.originalList = new ArrayList<AccomodationHeaderRow>();
        this.originalList.addAll(headerRows);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<AccomodationChildRow> childRows = headerRows.get(groupPosition).getChildRow();
        return childRows.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View view, ViewGroup parent) {

        AccomodationChildRow child = (AccomodationChildRow) getChild(groupPosition, childPosition);
        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.activity_accomodation_child_row, null);
        }

        TextView name = (TextView) view.findViewById(R.id.child_items);;
        name.setText(child.getName().trim());



        return view;
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        ArrayList<AccomodationChildRow> childRows = headerRows.get(groupPosition).getChildRow();
        return childRows.size();

    }

    @Override
    public Object getGroup(int groupPosition) {
        return headerRows.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return headerRows.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view,
                             ViewGroup parent) {

        AccomodationHeaderRow accomodationHeaderRow = (AccomodationHeaderRow) getGroup(groupPosition);
        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.activity_accomodation_header_row, null);
        }

        TextView heading = (TextView) view.findViewById(R.id.accomodation_headings);
        heading.setText(accomodationHeaderRow.getName().trim());

        // If group is expanded then change the text into bold and change the
        // icon
        if (isExpanded) {
            heading.setTypeface(null, Typeface.BOLD);
            heading.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    0, 0);
        } else {
            // If group is not expanded then change the text back into normal
            // and change the icon

            heading.setTypeface(null, Typeface.NORMAL);
            heading.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    0, 0);
        }

        return view;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void filterData(String query){

        query = query.toLowerCase();
        Log.v("Accomodation_Adapter", String.valueOf(headerRows.size()));
        headerRows.clear();


        if(query.isEmpty()){
            headerRows.addAll(originalList);
        }
        else {
            for(AccomodationHeaderRow accomodationHeaderRow: originalList){
                ArrayList<AccomodationChildRow> accomodationChildRows = accomodationHeaderRow.getChildRow();
                ArrayList<AccomodationChildRow> newList = new ArrayList<AccomodationChildRow>();
                for(AccomodationChildRow accomodationChildRow: accomodationChildRows){
                    if(accomodationChildRow.getName().toLowerCase().contains(query)){
                        newList.add(accomodationChildRow);
                    }
                }
                if(newList.size() > 0){
                    AccomodationHeaderRow nAccomodationHeaderRows =
                            new AccomodationHeaderRow(accomodationHeaderRow.getName(),newList);
                    headerRows.add(nAccomodationHeaderRows);
                }
            }
        }
        Log.v("Accomodation_Adapter", String.valueOf(headerRows.size()));
        notifyDataSetChanged();
    }

}

Post Reply