Hey guys what is the best way to search through my list of objects, they return a few strings, last name and first name for example.
Here how i'm currently searching but my search needs to match the entire string which I don't want. The search needs it to match part of the string like our contacts list on our phone and ignore the case.
if (searchQ.equalsIgnoreCase(child.first_name)) {
addChildToList(child);
}
Ive tried contains and starts with for example, they did not work. Whats going on?
Thanks! Cheers!
You can use a TextWatcher to search in a list, as an example you define a TextWatcher for an EditText(search box) in this way :
TextWatcher filterTextWatcher = new TextWatcher() { //TextWatcher to Filter RealTime Input Data
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
}
public void onTextChanged(CharSequence s,int start, int before,int count) //Filter Data When Entering Data On ItemType Dialog TextView
{
}
#Override
public void afterTextChanged(Editable arg0)
{
}
};
EditText filterText = (EditText)dialog.findViewById(R.id.edtItemFilter);
filterText.addTextChangedListener(filterTextWatcher);
then in onTextChange() callback function you can send inserted characters as parameter s to your adapter to filter your List with getFilter or setup your list with filtered Cursor :
public void onTextChanged(CharSequence s,int start, int before,int count)
{
ListView lv = getListView();
ListAdapter adapter = (ListAdapter) lv.getAdapter();
lv.setTextFilterEnabled(true);
adapter.getFilter().filter(s.toString());
}
Related
after the list is filtered the filtered items are duplicated
enter image description here
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
MainActivity.this.adapter.getFilter().filter(s);
}
});
Make sure that in your filter(String) function, you don't add items 2 times to the array used by the listview
Maybe you've done something like that:
if (item.getName().contains(string)) {
filter.add(item);
}
if (item.getDescription().contains(string)) {
filter.add(item);
}
I am trying to build a simple converter code(convert feet to mtr) in Android. I am able to do this but only when user clicks some button. Now I want to modify it such that it starts to convert as and when the user gives input(Something like google converter). Is there any way to do this in Android?
Thanks in advance.
Add the listener to your edittext:
yourEditText.addTextChangedListener(addTextWatcher);
Add the TextWatcher interface:
private TextWatcher addTextWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {
// here is where you could grab the contents of the edittext
// input each time a character is entered, and pass the value
// off to your unit conversion code. Careful to check for
// numerals/decimals only, or to set the proper inputType in
// your xml.
}
#Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable sequence) {
}
};
I have an SQLite database that contains a table that has 3 columns, I'm using an EditText and a ListView in my layout. What I've done by now is applying a search using the EditText and displaying the filtered results in a ListView.
The following code is applied to the EditText called myFilter. The words is the SimpleCursorAdapter:
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
words.getFilter().filter(s.toString());
}
});
What I want to do is to filter two CharSequences, for example I want to display in my ListView words that starts with a and b, can I do something like that (for example words.getFilter().filter("a","b"); ? Any help ?
In my activity there is an error in this line (Home.this,android.R.layout.activity_home, searchResults)); which says "activity_home cannot be resolved" but if i give as single_list_item_1 as mentioned in one of the tutorial, error disappears, for this error i had checked in res for capital letters, then cleaned the project but still not able to get rid of this.
searchBox.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//get the text in the EditText
String searchString=searchBox.getText().toString();
int textLength=searchString.length();
//clear the initial data set
searchResults.clear();
for(int i=0;i<songsList.size();i++)
{
String playerName=songsList.get(i).get("title").toString();
if(textLength<=playerName.length()){
//compare the String in EditText with Names in the ArrayList
if(searchString.equalsIgnoreCase(playerName.substring(0,textLength)))
Toast.makeText(getApplicationContext(),playerName,1).show();
searchResults.add(songsList.get(i));
}
list.setAdapter(new ArrayAdapter<HashMap<String, String>>
(Home.this,android.R.layout.single_list_item, searchResults));
}
adapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
public void afterTextChanged(Editable s) {
}
});
android.R.layout.activity_home
Something which starts with android is a predfined android resource.
if you want to use your own layout like activity_home.xml
you must use it this way
R.layout.activity_home
which means your application resource.
you have to learn the difference between android.R.layout.activity_home and R.layout.activity_home. the first one will try to load layout from android's predefined layout collection, while the other one is located in your projects layout folder
I am creating a comma seperated file and don't want to give the user a way to confuse the app.
Following what RoToRa said, you can delimitate the file using tabs instead.
If you do want to disallow commas, you can add a TextWatcher to modify the string before it is posted to the GUI:
EditText text;
private void foo()
{
text.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void afterTextChanged(Editable s)
{
// modify string here
}
});
}
Just check the addTextChangedListener(TextWatcher watcher) method - add the listener, which will check the editText field when it changes.