正如主题的名称所提到的,当我尝试实现实时ListView过滤时,当用户在搜索栏中输入一些位于操作栏中的文本时,我遇到了问题。 附:当我正在调试应用程序时,我甚至没有在日志中打印的onQueryTextChange()中使用Log.i(“Nomad”,“onQueryTextChange”):/
AllLists.java
public class AllLists extends Fragment implements SearchView.OnQueryTextListener{
private TypedArray navMenuIcons;
private Context context;
private int position = 0;
private String location;
private List<Item> items = new ArrayList<Item>();
private ListView listView;
private CustomListAdapter adapter;
private SearchView mSearchView;
public AllLists(Context context) {
this.context = context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.alllists, container, false);
View header = inflater.inflate(R.layout.list_header, null);
// View list_view = inflater.inflate(R.layout.list_item, null);
ImageView image = (ImageView) header.findViewById(R.id.small_icon);
header.setClickable(false);
// ImageView logo = (ImageView) list_view.findViewById(R.id.image);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
Bundle bundle = getArguments();
position = bundle.getInt("position");
location = bundle.getString("location");
image.setImageDrawable(navMenuIcons.getDrawable(position));
DatabaseHandler db = new DatabaseHandler(context);
items = db.getAllItems(location);
listView = (ListView) rootView.findViewById(R.id.list);
adapter = new CustomListAdapter(context, items);
listView.setAdapter(adapter);
listView.addHeaderView(header);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (location.equals("accommodation") || location.equals("eat") || location.equals("events")
|| location.equals("entertainment") || location.equals("places") || location.equals("cinema")) {
Intent i = new Intent(context, ItemScreen.class);
i.putExtra("position", position - 1);
i.putExtra("location", location);
startActivity(i);
}
}
});
setHasOptionsMenu(true);
return rootView;
}
/*SearchView.OnQueryTextListener(new SearchView.OnQueryTextListener){
}*/
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
return false;
}
});
}
@Override
public boolean onQueryTextChange(String s) {
Log.i("Nomad", "onQueryTextChange");
adapter.getFilter().filter(s);
return false;
}
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
}
CustomListAdapter.java
public class CustomListAdapter extends BaseAdapter {
private Context context;
private List<Item> items;
private LayoutInflater inflater;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private List<Item> filteredData;
public CustomListAdapter(Context context, List<Item> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView image = (NetworkImageView) convertView.findViewById(R.id.image);
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView type = (TextView) convertView.findViewById(R.id.type);
Item i = items.get(position);
// Log.d("FOTO linkas: " + i.getName() + " ", i.getIcon());
if (i.getIcon().contains("http")) {
// Log.d("suveike: ", i.getIcon());
image.setImageUrl(i.getIcon(), imageLoader);
}/*else if(items.get(0).getName().contains("taksi")) {
Log.d("suveike: ", "else if");
image.setImageResource(R.drawable.logo);
}*/ else
image.setImageResource(R.drawable.logo);
name.setText(i.getName());
type.setText(i.getType());
return convertView;
}
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
if (charSequence == null || charSequence.length() == 0) {
results.values = items;
results.count = items.size();
} else {
List<Item> filtered_items = new ArrayList<Item>();
for (int i = 0; i < items.size(); i++) {
if (items.get(i).getName().contains(charSequence))
filtered_items.add(items.get(i));
}
results.values = filtered_items;
results.count = filtered_items.size();
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredData = (List<Item>) filterResults.values;
notifyDataSetChanged();
}
};
}
}
答案 0 :(得分:0)
你必须在onQueryTextChange中的adapter.getFilter()。filter之后输入listView.setAdapter(adapter)。这将刷新listView。
答案 1 :(得分:0)
那么,你可以尝试这个..不要实现SearchView.onQueryTextChangeListner .. 而不是在onCreateView上,写下以下内容
mSearchView.setOnQueryTextListener(new OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//your code here
return false;
}
});