我在listview中搜索时遇到了一些麻烦。我使用Filter创建自定义数组适配器。如果结果正确,则它始终只显示第一个项目。当我使用基数组适配器时,结果显示为正确但我需要使用自定义适配器。你能帮助我吗?这是我的数组适配器代码:
public class ImageAdapter extends ArrayAdapter{
Activity context;
ArrayList<String> original;
ArrayList<String> fitems;
int layoutId;
int textId;
int imageId;
private Filter filter;
ImageAdapter(Activity context, int layoutId, int textId, int imageId, ArrayList<String> data){
super(context,layoutId,data);
this.context = context;
this.original = new ArrayList<>(data);
this.fitems = new ArrayList<>();
this.layoutId = layoutId;
this.textId = textId;
this.imageId = imageId;
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public Object getItem(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater=context.getLayoutInflater();
View row=inflater.inflate(layoutId, null);
TextView label=(TextView)row.findViewById(textId);
label.setText(original.get(position));
ImageView icon=(ImageView)row.findViewById(imageId);
icon.setImageResource(R.drawable.arrow);
return(row);
}
@Override
public Filter getFilter(){
if (filter == null)
filter = new ListFilter();
return filter;
}
private class ListFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint){
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if (prefix == null || prefix.length() == 0){
ArrayList<String> list=new ArrayList<String>(original);
results.values = list;
results.count = list.size();
}
else{
ArrayList<String> values = new ArrayList<String>(original);
ArrayList<String> nlist = new ArrayList<String>();
final int count = values.size();
for (int i=0; i<count; i++){
final String value = values.get(i);
final String valueText = values.get(i).toLowerCase();
if (valueText.startsWith(prefix)){
nlist.add(valueText);
}
else{
final String[] words = valueText.split(" ");
final int wordCount = words.length;
// Start at index 0, in case valueText starts with space(s)
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefix)) {
nlist.add(value);
break;
}
}
}
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
fitems = (ArrayList<String>)results.values;
notifyDataSetChanged();
clear();
for (int i=0; i<fitems.size(); i++){
String str = fitems.get(i);
add(str);
}
notifyDataSetInvalidated();
}
}
}
我用它:
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
if(EventForList.size()!=0) {
imAdapter2.getFilter().filter("");
}
else
Toast.makeText(MainActivity.this,"List is empty",Toast.LENGTH_SHORT).show();
}
else {
if(EventForList.size()!=0) {
imAdapter2.getFilter().filter(newText);
}
else
Toast.makeText(MainActivity.this,"List is empty",Toast.LENGTH_SHORT).show();
}