覆盖getView时启用过滤

时间:2012-10-09 19:16:55

标签: android listview view filter adapter

我有一个ListView,我正在覆盖getView方法,该方法正确填充以下代码。如果我使用'adapter.getfilter()',它不会更新列表视图,但在适配器中已过滤并在NEXT过滤器完成后正确显示'out()',在编辑框中输入文本。任何想法如何在我覆盖getView并同时过滤影响适配器而不是下次在编辑框中输入文本时启用过滤?

final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.activity_list_item,stringArray) 
{   @Override
public View getView(int position, View convertView, ViewGroup parent)
    {View  row= super.getView(position, convertView, parent);
        View row=convertView;
if(position==0){
row = mLayoutInflater.inflate(R.layout.list_header, null);
final TextView sectionView = (TextView) row.findViewById(R.id.list_item_section_text);
sectionView.setText(myList.get(position));
} 
else
{row = mLayoutInflater.inflate(R.layout.list_item, null);
final TextView sectionView = (TextView) row.findViewById(R.id.list_item_entry_title);
sectionView.setText(myList.get(position));}
return row;}});

lv.setAdapter(adapter);
for (int i = 0; i < adapter.getCount(); i++) {//displaying properly
pr("adapter list"+adapter.getItem(i));
}
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) { }
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
for (int i = 0; i < adapter.getCount(); i++) {
pr("adapter list before "+         (adapter.getItem(i)));//printing properly but not  
//updated in listview ,sometimes it just displays first few ,even if it should not be
//in filtered result after filtering.    
} }  });

1 个答案:

答案 0 :(得分:0)

我建议建立一个自定义过滤器。扩展Filter对象(我通常将其作为适配器的内部类)并进行过滤。保存传递给数组的项目的副本并从中进行过滤。在publishResults中清除适配器,然后重新添加过滤后的项目。