I have got a cursor that gets data out of the database and puts it in an AlertDialog. I want to add a search filter in this AlertDialog. Thus if a key is pressed, it has to find all the products with that character. I've tried it with a TextWatcher but it didn't work. Can you please help me out.
public void onClickSelectBrands(View view) {
builder=new AlertDialog.Builder(AddProduct.this);
builder.setTitle("Brands");
input = new EditText(this);
input.addTextChangedListener(filterTextWatcher);
builder.setView(input);
cursor = DatabaseHelper.fetchAllData("Brands");
builder.setCursor(cursor, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
// TODO Auto-generated method stub
bid = cursor.getString(cursor
.getColumnIndexOrThrow(DatabaseHelper.KEY_ID));
String name = cursor.getString(1);
brandName.setText(name);
}
}, DatabaseHelper.KEY_NAME);
dialog=builder.create();
dialog.show();
}
private TextWatcher filterTextWatcher = 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) {
((Filterable) builder).getFilter().filter(s);
}
};
@Override
public void onStop(){
input.removeTextChangedListener(filterTextWatcher);
}
0 comments:
Post a Comment