我正在尝试在自定义列表视图中添加搜索过滤器。几次我都尝试过,但是仍然有问题。在这里,搜索过滤器会直观地显示iin操作栏,但不起作用。任何人都可以解决此问题.....
Mainactivity.java
public class Sulphur extends AppCompatActivity {
private ListView listView;
private String[] productName;
private String[] genericName;
private String[] companyName;
private String[] apNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sulphur);
listView= findViewById(R.id.lv_Sulphur);
this.setTitle("Sulphur");
productName =getResources().getStringArray(R.array.Sulphur);
genericName =getResources().getStringArray(R.array.Sulphur_Generic);
companyName= getResources().getStringArray(R.array.Sulphur_Company);
apNumber=getResources().getStringArray(R.array.Sulphur_AP);
CustomAdapter adapter= new CustomAdapter(this, productName,genericName,companyName,apNumber);
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.layout_menu, menu);
MenuItem menuItem = menu.findItem(R.id.searchMenuID);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return true;
}
@Override
public boolean onQueryTextChange(String s) {
String text= s;
CustomAdapter.getFilter().filter(s);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
}
自定义适配器
public class CustomAdapter extends BaseAdapter {
String[] productName;
String[] genericName;
String[] companyName;
String[] apNumber;
Context context;
private LayoutInflater inflater;
CustomAdapter(Context context, String[]productName, String[]genericName,String[] companyName,String[] apNumber ){
this.context=context;
this.productName=productName;
this.genericName=genericName;
this.companyName=companyName;
this.apNumber=apNumber;
}
@Override
public int getCount() {
return productName.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view==null){
inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.sample_view,viewGroup,false);
}
TextView textView=view.findViewById(R.id.Product_textviewID);
TextView textView2=view.findViewById(R.id.Generic_texviewtID);
TextView textView3=view.findViewById(R.id.Company_texviewtID);
TextView textView4=view.findViewById(R.id.APNumber_texviewtID);
textView.setText(productName[i]);
textView3.setText(companyName[i]);
textView4.setText(apNumber[i]);
textView2.setText(genericName[i]);
return view;
}
public static android.widget.Filter getFilter(){
return new android.widget.Filter(){
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
return null;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
}
};
}
}