Reputation: 1141
Hey guys I need my search function to act like the contacts. I'm retrieving my list of participants from a server and I rebuild it when the user searches to get there results. Hoover i can only get it to work when they enter their full first name.. Let me show you my code. Ive tried a lot of combinations. any idea? This is a donation app for the child hunger alliance. =]
this is just in a for loop to add each name fyi
if (searchQ.contains(child.first_name.toLowerCase())
|| searchQ.matches(child.last_name.toLowerCase()) {
id = child.id;
addChildToList(child);
}
if (searchQ.matches("")){
addChildToList(child);
}
here is my textwatcher
public void searchChild(){
final EditText ET = (EditText) findViewById(R.id.search);
// create the TextWatcher
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// Rebuilds the list
String searchQ = ET.getText().toString();
getChildrenListSearch(searchQ);
}
@Override
public void afterTextChanged(Editable editable) {
// Remove rows that do no match
TableLayout tl = (TableLayout)findViewById(R.id.childList);
tl.removeAllViews();
}
};
//we must add the textWatcher to our EditText
ET.addTextChangedListener(textWatcher);
}
Upvotes: 1
Views: 116
Reputation: 4567
Other way around...
if (searchQ.contains(child.first_name.toLowerCase())
|| searchQ.matches(child.last_name.toLowerCase()) {
id = child.id;
addChildToList(child);
}
to this...
if (child.first_name.toLowerCase().contains(searchQ)
|| searchQ.matches(child.last_name.toLowerCase()) {
id = child.id;
addChildToList(child);
}
Upvotes: 1