搜索过滤器Expandablelistview标题项

时间:2017-03-09 07:33:34

标签: android android-studio expandablelistview

我即将使用已实现的Searchview创建一个expandableListview。我的SearchView可以过滤子项,只过滤子项。我想创建一个搜索过滤器,也可以过滤expandablelistview中的标题项。我怎么能这样做?

这是我的code,用于带有搜索过滤器的expandablelistview。 这是我的适配器:

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();
    }

}

提前谢谢。

0 个答案:

没有答案