Reputation: 3666
I got this piece of code:
public void cap_search(){
EditText search = (EditText) findViewById(R.id.search);
String str_search = null;
adapter = new ItemListAdapter(this, data);
setListAdapter(adapter);
str_search = search.getText().toString();
for (int i = data.size()-1; i >= 0; i--){
if (!data.get(i).getCaption().contains(str_search)){
data.remove(i);
}
}
}
I will explain this code, and what it does in my code:
People get to see a list of caps, they can check all caps which they own. But because I got a lot of caps, I build in a search function. So when they type: "A" then the list should only show the caps that contains an "A". Till this everything works, now the problem. After they type the: "A" I remove all items that do not contain it. But when the user delete the "A" and type a "B", then it should check the whole list (also the deleted ones that didn't start with an "A") but it doesn't, because I removed them.
So is it possible to make the items invisible instead of remove them? Or maybe another way to solve this problem?
I hope it is clear, if not please say it, then I will try to explain it different.
Thanks already, Bigflow
Edit 1:
Tried many answers but I just can't get it to work properly.
Someone got more ideas?
TextWatcher seems pretty cool, but it is too difficult for me, This is my second project I try to implement it, but also this time I can't get it to work.
I never used SQL too.
So someone else got any other answers? Or maybe someone that is good with textwatcher, could you make/help me make me one. Examples of textwatchers on the internet are too complicated for me most of the time. I would really appreciate it.
Edit 2:
I am still figuring this option/function out, but what about an use with adapter.getfilter();
?
Upvotes: 0
Views: 188
Reputation: 7967
Try using two lists, one with all the results and one with the search result. Display always the second list and if you want to display the whole list just point the second list to the first list. Something like:
private List<Item> displayData = new ArrayList<Item>();
public void cap_search(){
EditText search = (EditText) findViewById(R.id.search);
String str_search = null;
str_search = search.getText().toString();
displayData.clear();
if (str_search == null || str_search.equals("")) {
displayData.addAll(data);
} else {
for (int i = data.size()-1; i >= 0; i--){
if (data.get(i).getCaption().contains(str_search)){
displayData.add(data.get(i));
}
}
}
adapter = new ItemListAdapter(this, displayData);
setListAdapter(adapter);
}
Upvotes: 1
Reputation: 5390
Try to establish required conditions every time you use the list.
Upvotes: 0
Reputation: 169
I solved this with an "update Button" if this is a solution for you. With an empty search pattern, I just load the whole list again.
Upvotes: 0