Android的我如何覆盖过滤器我ArrayAdapter?过滤器、Android、ArrayAdapter

2023-09-04 04:15:40 作者:废途

嘿家伙我在这里的第一篇文章......我试着写一个自定义过滤器来过滤在我arrayadapter ArrayList中,这样我的列表视图过滤当我点击该按钮。

例如,当我点击我的按钮

 公共无效的onClick(查看为arg0){
            字符串ABC =ABC;
            。m_adapter.getFilter()过滤器(ABC);
        }
 

然而,当我点击我的按钮,我的应用程序意外终止。这是我的$ C $下一个ArrayAdapter和过滤。请帮我。

 包com.ntu.rosemobile.searchlist;

公共类ResultsAdapter扩展ArrayAdapter< SearchItem>实现过滤的{

公众的ArrayList< SearchItem>子项目;
公众的ArrayList< SearchItem>所有项目;
私人LayoutInflater充气;
私人PTypeFilter过滤器;

公共ResultsAdapter(上下文的背景下,INT textViewResourceId,ArrayList的< SearchItem>项目){

    超(背景下,textViewResourceId,项目);
        this.subItems =项目;
        this.allItems = this.subItems;
        充气= LayoutInflater.from(上下文);
}

@覆盖
公共过滤用getFilter(){
    如果(过滤== NULL){
      过滤器=新PTypeFilter();
    }
    返回过滤器;
  }



// @覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
        视图V = convertView;
        如果(V == NULL){

            V = inflater.inflate(R.layout.listrow,NULL);
        }
        SearchItem O = subItems.get(位置);
        如果(O!= NULL){
                TextView的PNAME =(TextView中)v.findViewById(R.id.productname);
                TextView的NEG =(TextView中)v.findViewById(R.id.negNum);
                TextView的POS =(TextView中)v.findViewById(R.id.posNum);
                TextView的神经=(TextView中)v.findViewById(R.id.neuNum);

                WebImageView productPhoto =(WebImageView)v.findViewById(R.id.pPhoto);
                如果(productPhoto!= NULL){
                    productPhoto.setImageUrl(o.getImageUrl()的toString());
                    productPhoto.loadImage();
                }
                如果(PNAME!= NULL){
                    pname.setText(o.getProductName()的toString());
                }
                如果(NEG!= NULL){
                      字符串=+ o.getNegativeReviews();
                      neg.setText(一);
                }
                如果(神经!= NULL){
                     字符串=+ o.getNeutralReviews();
                     neu.setText(一);
                }
                如果(POS!= NULL){
                    字符串=+ o.getPositiveReviews();
                    pos.setText(一);
                }
        }
        返回伏;
}

私有类PTypeFilter扩展过滤器{


    @燮pressWarnings(未登记)
    @覆盖
    保护无效publishResults(CharSequence的preFIX,
                                  FilterResults结果){
      //注:此功能*总*从UI线程调用。
       子项目=(ArrayList的< SearchItem>)results.values​​;

        notifyDataSetChanged();
    }

    @燮pressWarnings(未登记)
    保护FilterResults performFiltering(CharSequence的preFIX){
          //注:此功能是*总*从后台线程调用,
          //没有UI线程。

          FilterResults结果=新FilterResults();
          ArrayList的< SearchItem> I =新的ArrayList< SearchItem>();

          如果(preFIX = NULL和放大器;!&安培; prefix.toString()长度()0){

              对于(INT指数= 0;指数< allItems.size();指数++){
                  SearchItem SI = allItems.get(指数);
                  如果(si.getPType()的compareTo(prefix.toString())== 0){
                    i.add(SI);
                  }
              }
              results.values​​ =我;
              results.count = i.size();
          }
          其他{
              同步(allItems){
                  results.values​​ = allItems;
                  results.count = allItems.size();
              }
          }

          返回结果;
    }
  }
}
 

解决方案 Array Plot with Android Devices

您需要覆盖在一个ArrayAdapter类的getCount将()方法。

Hey guys my first post here... Im trying to write a custom filter to filter the arraylist in my arrayadapter such that my listview is filtered when i click on the button.

For instance when i click on my button

public void onClick(View arg0) {
            String abc = "abc";
            m_adapter.getFilter().filter(abc);
        }

However, when i click on my button, my app terminate unexpectedly. Here is my code for the arrayadapter and filter. Please help me.

package com.ntu.rosemobile.searchlist;

public class ResultsAdapter extends ArrayAdapter<SearchItem> implements Filterable{

public ArrayList<SearchItem> subItems;
public ArrayList<SearchItem> allItems;
private LayoutInflater inflater;
private PTypeFilter filter;

public ResultsAdapter(Context context, int textViewResourceId, ArrayList<SearchItem> items) {

    super(context, textViewResourceId, items);
        this.subItems = items;
        this.allItems = this.subItems;
        inflater= LayoutInflater.from(context);
}

@Override
public Filter getFilter() {
    if (filter == null){
      filter  = new PTypeFilter();
    }
    return filter;
  }



//@Override
public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {

            v = inflater.inflate(R.layout.listrow, null);
        }
        SearchItem o = subItems.get(position);
        if (o != null) {
                TextView pname = (TextView) v.findViewById(R.id.productname);
                TextView neg = (TextView) v.findViewById(R.id.negNum);
                TextView pos = (TextView) v.findViewById(R.id.posNum);
                TextView neu = (TextView) v.findViewById(R.id.neuNum);

                WebImageView productPhoto = (WebImageView)v.findViewById(R.id.pPhoto);
                if(productPhoto!=null){
                    productPhoto.setImageUrl(o.getImageUrl().toString());
                    productPhoto.loadImage();
                }
                if(pname!= null){
                    pname.setText(o.getProductName().toString());
                }                    
                if (neg != null) {
                      String a =  "" + o.getNegativeReviews();
                      neg.setText(a);                            
                }
                if(neu != null){
                     String a =  "" + o.getNeutralReviews();
                     neu.setText(a);
                }
                if(pos != null){
                    String a =  "" + o.getPositiveReviews();
                    pos.setText(a);
                }
        }
        return v;
}

private class PTypeFilter extends Filter{


    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence prefix,
                                  FilterResults results) {
      // NOTE: this function is *always* called from the UI thread.
       subItems =  (ArrayList<SearchItem>)results.values;

        notifyDataSetChanged();
    }

    @SuppressWarnings("unchecked")
    protected FilterResults performFiltering(CharSequence prefix) {
          // NOTE: this function is *always* called from a background thread, and
          // not the UI thread. 

          FilterResults results = new FilterResults();
          ArrayList<SearchItem> i = new ArrayList<SearchItem>();

          if (prefix!= null && prefix.toString().length() > 0) {

              for (int index = 0; index < allItems.size(); index++) {
                  SearchItem si = allItems.get(index);
                  if(si.getPType().compareTo(prefix.toString()) == 0){
                    i.add(si);  
                  }
              }
              results.values = i;
              results.count = i.size();                   
          }
          else{
              synchronized (allItems){
                  results.values = allItems;
                  results.count = allItems.size();
              }
          }

          return results;
    }
  }     
}

解决方案

You need to override the getCount() method in the ArrayAdapter class.