My Friends, I have two issues. I'm pulling out data from database and populated them in a listview. The filtering is avaible:
1) When I use filter and go to another activity and try to go back to the listview I'm getting an Exception.
2) I can't use filter when I just pick an item from list without filtering and go back, then everything is fine when filter is not used, but I can't use it afterwords (doesn't filter, no errors) I need to this activity again and then filtering works again.
As for issue number one if instead of ManagingTheCursor i just close it, then the listview doesn't populate at all at the bigining I need to use filter to get any result and it shows desired result. But when and I just delete the text from the filter it shows a whole list. Then I can choose an item click on it, go back, but then it doesn't filter again. I'd like it to populate list without using the filter.
Resuming I'd like filter to work even after clicking on the item from the list and to have database populated in the listview without need of using filter to get any result.
That's the code:
private void populateListViewDB() { Cursor cursor = myDB.getAllRows();
String[] fromFieldNames = new String[]
{DBAdapter.KEY_NAME, DBAdapter.KEY_COUNTRY, DBAdapter.KEY_REGION, DBAdapter.KEY_PHONE};
int [] toViewIDs = new int []
{R.id.list_item_name, R.id.list_item_country, R.id.list_item_region, R.id.list_icon_item};
final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
this, //context
R.layout.list_item,
cursor,
fromFieldNames,
toViewIDs
);
ListView myList = (ListView) findViewById(R.id.listViewDB);
myList.setAdapter(myCursorAdapter);
myList.setTextFilterEnabled(true);
EditText myFilter = (EditText) findViewById(R.id.ETSearch);
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) {
myCursorAdapter.getFilter().filter(s.toString());
}
});
myCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return DBAdapter.fetchCountriesByName(constraint.toString());
}
});
cursor.close();
}
Thank you for your time.
0 comments:
Post a Comment