final String[] defaulttask1 = {"abc"," def"," ghi"};
final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)this.findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,defaulttask1);
autoCompleteTextView.setThreshold(0);
autoCompleteTextView.setAdapter(adapter);
I have a method using autoCompleteTextView.getText().toString().While the user types words which is not in the array, it may happen "null pointer exception"
Is there any solution that I can use autoCompleteTextView, that can search for user and let user enter text they want?
P.S. sorry, I'm not a native speaker, there might be some grammar mistake.
AutoCompleteTextView is right solution for your needs
An editable text view that shows completion suggestions automatically
while the user is typing. The list of suggestions is displayed in a
drop down menu from which the user can choose an item to replace the
content of the edit box with.
AutoCompleteTextView will allow user to type whatever he/she want but will give suggestions from array.
autoCompleteTextView.getText().toString()
will not return null in case of user types words which is not in the array. It will return whatever text is there right now in text view. It might crash if you do getText().toString() on a empty text view. So have a check before you convert textview to string
if(autoCompleteTextView.getText()!=null)
String textviewString = autoCompleteTextView.getText().toString()
Related
I have a dynamic arraylist that contains String values. I want to match one of its values with AutocompleteText whatever the user has typed/entered. In case the value is not matching with any of the Arraylist values I would like to clear Autocomplete Text.
Couple of Ways I have tried.
Match the typed or user selected value from autocomplete text with list directly without any loop using "contains" method. Didn't achieve the expected result.
Store the values in String[] array from list and loop through it and match it with user input/selection from Autocompletetext. Didn't achieve the result this way too.
Please provide any ideas on how to clear text from autocompletetextview if value is not found?
You did not specify any language that's why i will answer in kotlin
Override onKeyDown method and let's say you will check when enter pressed check if given keycode value equals to KeyEvent.KEYCODE_ENTER
In if block get your text and use an algorithm like below
val arr = intArrayOf(1,2,3) if (textFromTextView !in arr) it means your typed text not in your array and if this condition is true
use autoCompleteTextView.replaceText("") and i hope it will work for you.I never tried but i searched for you.
How to set the string values on spinner prompt?
The process how I am doing is:
I am able to retrieve the json data(it has only one single value), so I am getting the single text with the help of String.
Later, I am trying to set the string value in Spinner prompt
SP_gender is called as Spinner here. In String gender1, I have texts called as "male"
String gender1 = i.getStringExtra("bundle_CusGender");
SP_gender.setPrompt(gender1);
System.out.println("Check bundle_CusGender = : " + gender1);
When I try to print this, I am getting a value as male
System.out: Check bundle_CusGender = : male
How should I set the single text in spinner Android?
Firstly the setPrompt() methods documentation states:
Sets the prompt to display when the dialog is shown.
Which is pretty vague but internally it calls the setPromptText() method which sets the description on the popup and has the following documentation.
Set hint text to be displayed to the user. This should provide
a description of the choice being made.
So if I am understanding your question correctly you want to set the spinners selected item. You should be using either setSelection(int) or setSelection(int, boolean)
For those to work you would need to give your spinner an adapter if you haven't already. If you don't know how you should check out this answer which explains in more detail.
I have Auto-complete Box in Android, i am filling this Auto-complete Box with the names of Some list retrieved from the Server.
Suppose i Have ABC, XYZ, PQR, ABCC, ABCCD etc... Now on First Suggestion when i type AB: It should give me ABC, ABCC, ABCCD. Now i have selected ABC..
(Autocomplete in multivalue separated by ;)
NOw when i write ABC: It Again give ABC, ABCC, ABCCD..
Result i Want: on ABC select should be removed from the Autocomplete list. so Next suggestion should be ABCC, ABCCD only not ABC.
Please Help me out
Thanks in Advance..
Code from comment below:
myAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.talksender);
myAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
myAutoComplete.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, names));
You need to keep track of the selected items.
When creating the filtered list you need to filter by the string typed into the EditText AND by the items already selected.
For filtering your ArrayAdapter you need to:
Create a Filter implementaion which performs the filtering based on the EditText input AND the already selected items.
Override your ArrayAdapter's getFilter() method to return your custom Filter implementation
WHAT I HAVE :
An AutocompleteTextView *txt_itemName* attached to an array adapter to show suggestions while typing.
ArrayAdapter<String> autoCompAdapter = new ArrayAdapter<String>(HomePageActivity.this,
R.layout.listrow_item_autocomp, items_list);
txt_itemName.setAdapter(autoCompAdapter);
WHAT I NEED TO KNOW
While typing, if there is no match found, it should inform the user, thru the autoCompleteTextView itself or by some other way
Any suggestions ??
I have this problem. I read with BufferedReader text from one system file, this text contains for example 5 WORDS, but in another case it can contain less or more words, then I put this text (these words) to ONE string and save that string to shared preferences. Then I make spinner from this string,
Code here:
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, yourString.split(" "));
spinner.setAdapter(spinnerArrayAdapter);
And now if for example spinner contains 5 options (5 words) and user select some of these words, I need to put this word to one system file. I use echo command for insertion. So the best thing would be if I could save chosen word from spinner to shared preferences as string. I use if(possition==0) for selection in normal spinner, but I think it's not possible to use it in this case.
Could anybody help me?
you can get selected text by writing this line of code
spinner.getSelectedItem().toString();
SharedPreferences pref = getSharedPreferences(
"Preferences", 0);
SharedPreferences.Editor edit = pref.edit();
edit.putString("ABC", spinner.getSelectedItem().toString());
edit.commit();
not getting your complete idea what you want to do...
but i think you want to get text which is selected in the spinner, then split your string in one array pass this array in your spinner adapter.
After that use onItemSelection method, In this method u will get position and fetch the corresponding record from your array.
Try using the getPosition() method on the ArrayAdapter.
spinnerArrayAdapter.getPosition(savedstring);
If the saved string is in the array second time in then you can call setSelection() to that position otherwise let the user know (e.g. via Toast) that the previous selection is no longer valid.