0

我的 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);

}

}

4

2 回答 2

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);

        }
于 2013-01-26T11:34:35.763 回答
0

试着放

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

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

假设this.adapter是您 visibleListView的实际适配器。

于 2013-01-26T12:00:24.590 回答