在我的应用中,我从sqlite获取数据并在列表视图中显示数据。现在,我想过滤列表视图。为此,我实现了一个搜索小部件,但它给出的结果不正确。这是我的代码:
<item
android:id="@+id/menu_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:title="@string/menu_search"/>
我的onCreateOptionsMenu
中包含此代码....
SearchManager searchManager = (SearchManager) getSystemService(this.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
.getActionView();
if (null != searchView) {
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
SearchView.OnQueryTextListener queryTextListener = new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// default adapter
adapter.getFilter().filter(newText);
return false;
}
};
searchView.setOnQueryTextListener(queryTextListener);
....
我的适配器代码是
public class AmbulanceAdapter extends ArrayAdapter<String> {
Context context;
String[] namesArray, areasArray, contactsArray;
public AmbulanceAdapter(Context c, String[] Names,
String[] Areas, String[] Contacts){
super(c, R.layout.row_ambulance, R.id.nametv, Names);
this.context = c;
this.namesArray = Names;
this.areasArray = Areas;
this.contactsArray = Contacts;
}
class viewHolder {
TextView nametv, areatv, contacttv;
public viewHolder(View v) {
nametv = (TextView) v.findViewById(R.id.nametv);
areatv = (TextView) v.findViewById(R.id.areatv);
contacttv = (TextView) v.findViewById(R.id.contacttv);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
viewHolder holder = null;
if (row == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflator.inflate(R.layout.row_ambulance, parent, false);
holder = new viewHolder(row);
row.setTag(holder);
} else {
holder = (viewHolder) row.getTag();
}
holder.nametv.setText(namesArray[position]);
holder.areatv.setText(areasArray[position]);
holder.contacttv.setText(contactsArray[position]);
return row;
}
}
但是我的搜索结果出错了,哪里出错?