我可以使用searchview过滤listview,它工作正常。我的listview每秒更新一次,这个处理程序用于更新适配器。
private void nonstoprun() {
handler = new Handler();
update = new Runnable() {
@Override
public void run() {
musers = (ArrayList<mobstat>) mobstat.listAll(mobstat.class);
descAdapter = new DescAdapter(seconds.this, musers, seconds.this);
contlist.setAdapter(descAdapter);// Notify our update
handler.postDelayed(this , 1000);
// android.widget.Filter filter = descAdapter.getFilter();
//filter.filter(searchView.getQuery());
}
};
handler.postDelayed(update, 10);
}
因此,在我在searchview中输入字符串后,列表视图会被过滤,但会立即显示完整的列表视图,因为它不会保留过滤后的值。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
android.widget.Filter filter = descAdapter.getFilter();
filter.filter(newText);
return true;
}
});
//searchView.setSubmitButtonEnabled(true);
return true;
}
Myadapter:
public class DescAdapter extends BaseAdapter implements Filterable {
private seconds ds;
private ArrayList<mobstat> musers;
private ArrayList<mobstat> mOrig;
Activity seconds;
public DescAdapter(seconds ds, ArrayList<mobstat> musers, Activity seconds) {
this.ds = ds;
this.musers = musers;
this.seconds = seconds;
}
@Override
public int getCount() {
return musers.size();
}
@Override
public Object getItem(int i) {
return musers.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
mobstat du = musers.get(i);
String name = du.name;
String status = du.status;
Viewholder viewholder;
if(view==null) {
viewholder = new Viewholder();
view = LayoutInflater.from(ds).inflate(R.layout.custom, null);
viewholder.uname = (TextView) view.findViewById(R.id.name);
viewholder.ustatus = (TextView) view.findViewById(R.id.status);
view.setTag(viewholder);
} else { viewholder = (Viewholder) view.getTag(); }
viewholder.uname.setText(name);
viewholder.ustatus.setText(status);
return view;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<mobstat> results = new ArrayList<mobstat>();
if (mOrig == null)
mOrig=musers;
if(constraint!=null){
if(mOrig!=null && mOrig.size()>0){
for(final mobstat g : mOrig){
if (g.getname().toLowerCase().contains(constraint.toString())){
results.add(g);}
}
}
oReturn.values=results;
}
return oReturn;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
musers= (ArrayList<mobstat>) results.values;
notifyDataSetChanged();
}
};
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
为什么过滤列表不能保持均匀 - 尽管适配器值每秒都在更新。
答案 0 :(得分:0)
我发现需要从服务器更新值。您可以为适配器编写一个新的构造函数作为参数传递查询字符串(如果它不是空的可能)。 过滤构造函数中的值应该有效。
如果您在构造函数之后过滤值,您可能会在UI中出现故障,因为首先使用所有值初始化适配器然后过滤