我正在尝试提供可搜索的列表视图。每个列表项都有一个名称和地址文本框,但我只想过滤名称。我当前的代码什么都不做,即根本没有过滤。有没有办法设置要过滤的列?
//class variables
private SimpleCursorAdapter mAdapter;
private EditText filterText = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_customer_listview);
//listViewCustomers = (ListView) findViewById(R.id.list);
buildingListViewAdaptor();
setListAdapter(mAdapter);
// set up the filter
filterText = (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
mAdapter.getFilter().filter(s);
Log.d(GlobalTools.ErrorCodes.INFO, "Searchtext=" + s.toString());
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
};
private void buildingListViewAdaptor(){
//1. Get the data
CustomerLocationDataHandler clDataHandler = new CustomerLocationDataHandler(getContentResolver());
Cursor cursor = clDataHandler.allCustomerLocations();
clDataHandler=null;
//2. Build the adaptor
mAdapter = new SimpleCursorAdapter(this,
R.layout.list_item_custom_font, // was list_item_custom_font
cursor,
new String[]{MyobiliseData.Columns_CustomerLocations.CUSTOMER_NAME,MyobiliseData.Columns_CustomerLocations.CITY},
new int[] {R.id.text1,R.id.text2}
);
}