Java 在扩展BaseAdapter的customAdapter中实现getFilter

Java 在扩展BaseAdapter的customAdapter中实现getFilter,java,android,adapter,custom-adapter,Java,Android,Adapter,Custom Adapter,前提:我是Java和Android的新手,对此我已经搜索了很多,但我不明白如何在代码中实现getFilter 这是主要活动(相关代码): 如何实现此函数,使其能够在数组中搜索,并在listview中显示过滤后的结果 如果您需要任何其他信息,请询问!谢谢我认为您的适配器应该是这样的。此外,我不建议在getView()中实例化对象,即typeface对象 public class ListViewAdapter extends BaseAdapter { String title[];

前提:我是Java和Android的新手,对此我已经搜索了很多,但我不明白如何在代码中实现getFilter

这是主要活动(相关代码):

如何实现此函数,使其能够在数组中搜索,并在listview中显示过滤后的结果


如果您需要任何其他信息,请询问!谢谢

我认为您的适配器应该是这样的。此外,我不建议在getView()中实例化对象,即typeface对象

 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;

     }

    }
公共类ListViewAdapter扩展了BaseAdapter{
字符串标题[];
字符串描述[];
ArrayList filteredTitleList;
ArrayList FilteredDescriptionList;
字体;
充气机;
公共ListViewAdapter(活动上下文,字符串[]标题,字符串[]说明){
超级();
this.context=上下文;
this.title=标题;
this.description=描述;
typeface=typeface.createFromAsset(context.getAssets(),“font/Simple_Print.ttf”);
充气器=context.getLayoutFlater();
this.filteredTitleList=新的ArrayList();
this.filteredescriptiponList=新的ArrayList();
applyFilter(空);
}
公共无效应用过滤器(字符串过滤器){
filteredTitleList.clear();
FilteredDescriptionList.clear();
for(int i=0;i
我找到了一个解决方案并与您共享,我在MainActivity中的inputSearch中添加了addTextChangedListener:

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) {

      }
  });
inputSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i2,int i3){
}
@凌驾
public void onTextChanged(CharSequence CharSequence,int i,int i2,int i3){
textLength=inputSearch.getText().length();
//allProd_排序和allSpec_排序是用于搜索的ArrayList
allProd_sort.clear();
allSpec_sort.clear();
String text=inputSearch.getText().toString();
//allProdString是从ArrayList allProd获取的字符串

对于(int y=0;yimplement
Filterable
接口。重写getFilter并过滤data@Raghunandan好的。你能给我举个例子吗?让两组数据在搜索时显示过滤后的数据,在没有搜索时显示原始数据。你可以引用这个@Raghunandan,所以我应该创建其他ArrayList并用过滤后的数据填充它data..但是这个新的ArrayList应该在哪里创建?在MainActivity中还是在CustomAdapter中?因为现在在MainActivity中用解析xml填充原始ArrayList这只是一个想法,您可以决定如何实现自定义筛选器
 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;

     }

    }
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) {

      }
  });