我的Android SearchView上没有过滤的代码提供了我的listview

时间:2015-08-06 04:35:23

标签: android listview filter searchview

我在这里创建了一个主要的类,当我运行过滤器时它不起作用,如果它请你怎么做,这是我的代码

public class List_escale extends Activity implements SearchView.OnQueryTextListener{
    // Refresh menu item
    private MenuItem refreshMenuItem;
    public static ArrayList<Escales> records=null;
    private SearchView mSearchView;
    Activity context=null;
    StringBuffer buffer=null;
    public static ProgressDialog pd;
    CustomAdapter adapter=null;
    TextView mSearchText;
     Filter filter;
    ListView lv=null;
    BackTask bt=new BackTask();
    protected void onCreate(Bundle savedInstanceState) {
    //TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_escale);
    config.Specific=false;
    getActionBar();
    lv=(ListView)findViewById(R.id.escales);
    lv.setTextFilterEnabled(true);
    context=this;
    records=new ArrayList<Escales>(); 
    adapter=new CustomAdapter(this, R.layout.list_item,R.id.LvDate,records);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,final int arg2,
                long arg3) {
            pd = new ProgressDialog(context);
            pd.setTitle("Detail escale");
            pd.setMessage("Please wait.");
            pd.setCancelable(true);
            pd.setIndeterminate(true);
              final View  v= arg1;
                    // TODO Auto-generated method stub
                    TextView label=(TextView) v.findViewById(R.id.LvNom_Navire);
                    String nom = (String) label.getText();
                    TextView date=(TextView) v.findViewById(R.id.LvDate);
                    String dates = (String) date.getText();
                    pd.show();
                    Intent intent = new Intent(List_escale.this, Detail_escale.class);
                    //send data to the next activity
                    intent.putExtra("nom_navire", nom);
                    intent.putExtra("date_arrivee", dates);
                    startActivityForResult(intent, 100);
        }
            });
    lv.setTextFilterEnabled(true);
    filter = adapter.getFilter();
    }}


    public void  onStart(){
        super.onStart();
        //execute background task
        if (config.etat==false) {
        bt.execute();
        }
        }

        private class BackTask extends AsyncTask<Void,Void,Void>{
            protected void onPreExecute(){
        super.onPreExecute();
    }

        protected Void doInBackground(Void...params){
            if (config.etat==false) {
                config.etat=true;

        getData();
            }
        return null;
        }

        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            return null;

    }

    protected void onPostExecute(Void result){

    Log.e("size", records.size() + "");
    adapter.notifyDataSetChanged(); //notify the ListView to get new records

    }

        }




        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
             getMenuInflater().inflate(R.menu.barre, menu);
              SearchManager searchManager = (SearchManager) 
getSystemService( Context.SEARCH_SERVICE );
                    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
                    searchView.setSearchableInfo(searchManager.
getSearchableInfo(getComponentName()));
                    searchView.setSubmitButtonEnabled(true);
                    searchView.setOnQueryTextListener(this);
            return super.onCreateOptionsMenu(menu);

    }
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {
            // Action to be taken after selecting a spinner item
                return false;
            }


        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub
            return false;
        }


         @Override public boolean onQueryTextChange(String newText) 
         { if (TextUtils.isEmpty(newText)) 
         { filter.filter(null); }
         else { filter.filter(newText); } 
         return true; }
    }

这是我的CustomAdapter类

public class CustomAdapter extends ArrayAdapter<Escales> implements Filterable {

int groupid;
static ArrayList<Escales> records;
Context context;
private Filter navireFilter;
ArrayList<Escales> origRecords;

public CustomAdapter(Context context, int vg, int id, ArrayList<Escales> records) {
super(context, vg, id, records);
this.context = context;
groupid = vg;
CustomAdapter.records = records; 

} 

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

public Escales getItem(int position) {
    return records.get(position);
}

public long getItemId(int position) {
    return records.get(position).hashCode();
}

@SuppressLint("ViewHolder")
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(groupid, parent, false);
TextView date = (TextView) itemView.findViewById(R.id.LvDate);
date.setText(records.get(position).getpNamenav());
TextView NomNav = (TextView) itemView.findViewById(R.id.LvNom_Navire);
NomNav.setText(records.get(position).getuDate()); 


if (position % 2 == 0) {
    itemView.setBackgroundColor(Color.parseColor("#A4A4A4"));
} else {
    itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}

return itemView;
}

public static void refresh() {

records.removeAll(records);



}

public void resetData() {
    records = origRecords;
}
@Override
public Filter getFilter(){
   return new Filter(){

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
             constraint = constraint.toString().toLowerCase();
             FilterResults result = new FilterResults();

                if (constraint != null && constraint.toString().length() > 0) {
                  List<Escales> founded = new ArrayList<Escales>();
                        for(Escales item: origRecords){
                            if(item.toString().toLowerCase().contains(constraint)){
                                founded.add(item);
                            }
                    }

                        result.values = founded;
                        result.count = founded.size();
                    }else {
                        result.values = origRecords;
                        result.count = origRecords.size();
                    }
            return result;


    }
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
           clear();
           for (Escales item : (List<Escales>) results.values) {
                 add(item);
           }
    notifyDataSetChanged();

    }

    };
    }
}

这是我的自行车课程

public class Escales {
     private String NomNav;
      private String DateArriv;
      public void setNomNav(String NomNav){this.NomNav=NomNav;}
      public void setDate(String date){this.DateArriv=date;}
      public String getpNamenav(){return NomNav;}
      public String getuDate(){return DateArriv;}

    }

当我完全输入文本列表视图时,请帮助我,我的过滤器不起作用

0 个答案:

没有答案