在ListView中搜索atr Android

时间:2013-01-26 11:27:11

标签: android

我使用TextChangedListener进行搜索的代码不起作用。我在ListView中没有任何变化你可以说我的问题在哪里?我在本文http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/中阅读了这篇文章,在其他材料中我也尝试了

    list.setTextFilterEnabled(true);
    list.setFilterText(s.toString());

但它也不起作用

code:

   public class SicksListActivity extends Activity implements TextWatcher{
private DBHelper dbHelper;
private SimpleCursorAdapter adapter;
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sickness_list); 
    displaySicks();
}
private void displaySicks(){
    Queries queries=new Queries();
    Cursor c=queries.getAllSicks(this);
    ListView list=(ListView)findViewById(android.R.id.list);
    EditText inputText=(EditText)findViewById(R.id.sick_input_search);
    inputText.addTextChangedListener(this);
    String [] colums={SicknessDB.NAME};
    int [] views={android.R.id.text1};
    adapter=new SimpleCursorAdapter(this,
            android.R.layout.simple_expandable_list_item_1, c, colums,views);
    list.setAdapter(adapter);
}

public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub

}
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub
    this.adapter.getFilter().filter(s);

}

}

2 个答案:

答案 0 :(得分:0)

我没有实施,因为你正在实施,但我已经多次这样做了,它完美无缺:

public void onTextChanged(CharSequence s,int start, int before,int count)   //Filter Data When Entering Data On EditText
        {
            Cursor FilteredListCursor = YourDBAdapter.instance.CursorFilteredData(s.toString());    //Retrieve Filtered Cursor
            ZoneCardListadapter.changeCursor(FilteredListCursor);

        }

答案 1 :(得分:0)

尝试放

list.invalidateViews();
this.adapter.notifyDataSetChanged();

this.adapter.getFilter().filter(s);

假设this.adapter是您可见的ListView的实际适配器。