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
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) {
}
};
This is my first time asking a question here, as most of the time I find the answer I need within these forums. However I've been searching for ages and have had no luck so thought I would just ask.
My issue is when I call an external function it throws an error "The method processInput()is undefined for the type new TextWatcher(){}" my code is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText myTextBox = (EditText) findViewById(R.id.screenDisplay);
myTextBox.addTextChangedListener(new TextWatcher() {
// Not used but required for method to work correctly
public void afterTextChanged(Editable s) {}
// Not used but required for method to work correctly
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
// Processing input as entered into screen
public void onTextChanged(CharSequence s, int start, int before, int count)
{
processInput();
}
});
}
and the external method I'm trying to call is processInput(){}. The code for the function wrapper is shown below:
public void ProcessInput(String input){}
Hopefully its not something silly like a missing bracket that I'm overlooking,
Thank you in advance.
processInput();
instead try some thing like this. Make sure function name starts with small letter.
public void onTextChanged(CharSequence s, int start, int before, int count)
{
ProcessInput(s.toString());
}
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());
}
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.