I have a EditText which works with a TextWatcher. It is for searching in a database. When the user types more than one charackter into the EditText, the db query searchs for results and puts the results into a ListView. But if the user types something in and then delete all charackters, the ListView shows nearly all results of the db... thats my problem. I want, that then nothing is shown anymore.
TextWatcher:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count <= before) {
searchGrammar.clear();
} else if (count >= before) {
searchGrammar.clear();
}
}
@Override
public void afterTextChanged(Editable s) {
if (s == null) {
searchGrammar.clear();
} else {
sText = etSearch.getText().toString();
}
populateSearchList(db.getSearchData(sText));
populateListView();
registerClickCallback();
}
Methods:
private void populateSearchList(Cursor result) {
while (result.moveToNext()) {
searchGrammar.add(new SearchList((result.getString(1)), (result.getString(2))));
}
}
private void populateListView() {
ArrayAdapter<SearchList> sAdapter = new SearchListAdapter();
ListView sList = (ListView) findViewById(R.id.main_lV);
sList.setAdapter(sAdapter);
}
private class SearchListAdapter extends ArrayAdapter<SearchList> {
public SearchListAdapter() {
super(MainActivity.this, R.layout.content_grammar_search, searchGrammar);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.content_grammar_search, parent, false);
}
SearchList currentGrammar = searchGrammar.get(position);
TextView headT = (TextView) itemView.findViewById(R.id.cgs_tVHead);
headT.setText(Html.fromHtml(currentGrammar.getContent()));
TextView examplT = (TextView) itemView.findViewById(R.id.cgs_tVExpl);
examplT.setText(Html.fromHtml(currentGrammar.getExample()));
return itemView;
}
}
SQLite query:
public Cursor getSearchData(String content) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + COL_2 + " LIKE '" + content + "%' LIMIT 5", null);
return result;
}
I noticed, that the ListView is showing then (after input and deleting) all the time the same results, not even new ones.
Thank you very much in advance
Aucun commentaire:
Enregistrer un commentaire