I wish I had a notification when a spinner is activated and the dropdown list is displayed, but I cannot find one. When it is displayed for the first time, I want to change an entry in the list. I am using an ArrayAdapter with the spinner. Thank you.
I dunno if i understand you right.
But just set a flag, like this:
boolean isTriggeredBefore = false;
And if that is false just use another values for that arrayadapter for e.g
if(isTriggeredBefore)
ArrayAdapter(this, R.array.list, R.id.tv);
else
ArrayAdapter(this, R.array.listFirstTime, R.id.tv);
Hope that helps!
Related
i want to disable dropdown list of spinner if it have only one item, i have almost tried everything but still i didn't get the solution, some times i got nullpointerexception while trying any solution, here is my code.
ArrayAdapter<String> adapter =new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,str_fname);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_childName.setAdapter(adapter);
if(str_fname.length==1){
sp_childName.setBackground(null);
sp_childName.setClickable(false);
tv_childClassname.setText(str_className[0]);
}else{
sp_childName.setOnItemSelectedListener(new onItemSelected());
}
I've read many of the previous posts on this topic, but i'm not getting it right.
I have an adapter which has a private values of the items in the list.
When i update the values(add a new items), i watch the value in debugger and the "getView" func and see that the value is correct.
BUT the actual rowView i see is just the first item in the list.
I have no clue what may cause this.
This listview is on the same activity while i show a different layout and hide the listview to add a new item.
Can there be a connection while the listview visibility is "GONE"?
When i remove items from it it updates listview fine(that is done when listview is visible).
private void updateAdapter() {
this.values.clear();
this.values.addAll(staticlistIndifferentclass);
notifyDataSetChanged();
}
~~~~UPDATE~~~~
Ok,
So i discovered the cause of the problem, though i'm not sure why it is.
The code was fine the way it was with regular Listview but the bug is on:
com.baoyz.swipemenulistview.SwipeMenuListView
Try use ListView.invalidateViews
This should cause views rebuilding
Use new Adapter object like this :
listView.setAdapter(new yourCustomAdapter());
When you delete all dataSet items ,it is better to bind new Adapter object .
PROBLEM
My problem is that after changing data in my SpinnerArrayAdapter my Spinner does not react to item clicks on dropDownList.
However after orientation change occurs everything is working fine(?!).
EDIT: I noticed it does catch the item clicks but not representing/showing it on Spinner. Because after orientation change the selected item appears on Spinner
CONSTRUCTION
I have AutoCompleteTextView(ACTV) that's connected to AutoCompleteAdapter implementing Filterable. After entering some data into ACTV the result is passed to SpinnerArrayAdapter that is connected to Spinner.
There is a customListener set on AutoCompleteAdapter that is connected to SpinnerArrayAdapter and responsible for passing data between them.
Reason behind such construction is that user can have a 2-step choice. One on drop-down when choosing the data from ACTV and second one in case he change his mind.
So you can put POSTCODE in the ACTV select province that's connected to and change province when you miss-clicked/changed mind without forcing to enter POSTCODE again.
CODE
This is the part that is responsible for data change inside SpinnerArrayAdapter.
#Override
public void setCitiesFromPostcode(ArrayList<String> cities) {
this.clear();
this.addAll(cities);
notifyDataSetChanged();
}
I had a similar problem with ArrayAdapter. I just changed it to BaseAdapter and it works. Don't really know what the reason is, but it's somewhere in the implementation of ArrayAdapter.
Two common causes for this:
Event though it may look big enough, if your Spinner is too small it may not be able to display the value, confirm this by hardcoding the Spinner width and height to something large. Using a custom spinner item layout may help if this is the problem.
You're using custom objects in your array, not simple Strings or numbers that can be converted to strings. Use a custom class MyAdapter extends BaseAdapter implements SpinnerAdapter { } class in this case.
I currently have a whole bunch of spinners in my application. I wasn't thinking much and for each spinner, the first index is filled with N/A. It's not a big deal, but I was hoping to make it a bit cleaner.
I was wondering if there was a way that instead of calling setSelection( index ), I wanted to know if there was a way to set a Spinner to have nothing selected.
Basically, I want the spinner to have a list of items within it, but display none of them until you click on it. I also don't want there to be a blank index at the top.
I think its quite possible that there is no way to do this, so if anyone is sure of this, please let me know.
Thanks.
Its better to put first position as blank, so that it can be seen as nothing selected.
spinners have to have a selected state. i would recommend having a "no selection" option
Another option is to have the bottom button selected and then set that one to be invisible (the reason i set the last one to selected is so that you dont have a chunk of blank space in your app as that looks a bit trashy):
case SPINNER_OPTION_FIRST:
yourSpinnerLabel.setVisibility(View.VISIBLE);
yourTextField.setVisibility(View.VISIBLE);
break;
// do this for all your other ones if you want
case SPINNER_OPTION_LAST:
yourSpinnerLabel.setVisibility(View.GONE);
yourTextField.setVisibility(View.GONE);
maybe not the most efficient but it works so i hope that helps (still new so cut me some slack)!
Modifying the list which holds data can lead lots of errors or tough to manage the data.
Instead of manipulating the list,you can add item in adapter with NONE item.
Like this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
adapter.add("NONE");
adapter.addAll(products);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
I tried
// getSelectedItemPosition() returns -1 if there is nothing selected
spinner.setSelection(-1);
but it failed [spinner was setting 1st element from list].
I added "empty position" to spinner data which mimics that "nothing is selected". Note that 'empty position' is not visible on 'drop down' list.
BTW. Sorry for C#, hope the concept is clearly visible.
Custom adapter:
class CustomAdapter<T> : ArrayAdapter<T> {
public override int Count {
get { return base.Count - 1; }
}
}
Init spinner:
var data = new List<string> { "elem 1", "elem 2", "" };
spinner.Adapter = new CustomAdapter<String>(this, Resource.Layout.SimpleSpinnerItem, data);
spinner.SetSelection(2);
As the title, how can I remove the filtering on an ArrayAdapter used by an AutoCompleteTextView to get the original list back?
A little more background:
It all started from the sad fact that the "position" value passed in to onItemClick() is useless. The "position" refers to the position AFTER the array has been filtered, but I need to know its REAL position. So, what I'm trying to do is when I've got the text of the selected item (by using getItemAtPosition(position)), I compare it one-by-one with the original string array that backs the ArrayAdapter. However, I found that when onItemClick() is called, the adapter is already filtered, I no longer have access to the original array. So I thought if I can remove the filter, maybe I can get back the original array and look for the selected item in it.
ArrayAdapter<String> mAdapter;
public void onCreate() {
// Create an adapter and remembere it as a class member.
mAdapter = new ArrayAdapter<String>(this, layoutId);
// Add 100 strings to it and attach it to an AutoCompleteTextView
for (int i = 0; i < 100; i++)
mAdapter.add("random text");
((AutoCompleteTextView)findViewById(id)).setAdapter(mAdapter);
}
#Override
public void onItemClick(AdapterView<?> actv, View view, int position, long id) {
if (actv.getAdapter().equals(mAdapter))
Log.d("The adapter contained in actv is the same one I created earlier.");
// And, I can get the text of the item the user selected
String selected = (String)actv.getItemAtPosition(position);
// However, although the adapter passed in is still the same one, but the
// number of items in it is only 1! Because the array has been filtered.
int numItems = actv.getAdapter.getCount();
// So, I'm thinking if I can somehow remove the filtering here, then I can
// get back those 100 items, and do a search like following:
for (int i = 0; i < actv.getAdapter.getCount(); i++)
if (selected == actv.getAdapter.getItem(i))
break; // Eureka!!!
}
To tackle the problem of obtaining the REAL position of the selected item:
Is there a way to utilize the "id" value? Like, assign each item an id, then hopefully onItemClick() would pass back the correct id.
Like I said above, remove the filter (is it possible), get back the original 100 items, and perform a one-by-one search.
This is the last resort, I know it'll work, but I don't want to do it: Once I get the text of the selected text, I go back to the source of the data (from a database), query those 100 items out, and perform the search.
Another lame last resort: To avoid the overhead on accessing the database again as in #3, when in onCreate(), while creating the ArrayAdapter, I use an ArrayList of my own to remember all those 100 strings.
Am I doing it all wrong? What's the "right" way of obtaining the real position of the selected item from an AutoCompleteTextView?
Thank you very much!
(I read somewhere, some buy that seemed to be from Google Android team, said that one should use getFirstVisiblePosition() to resolve the position. But I can't figure out how.)
I don't know if you're still interested, but I found this answering a similar question: Problem with AutoCompleteTextView and Spinner using the same Adapter
Copying the method in the AutoCompleteTextView source code:
Filter filter = mAdapter.getFilter();
filter = null;
See my response in the above question for the grepcode link.
This is actually pretty simple to solve.. Instead of adding each element to the adapter as you get it (I'm assuming your random text part is just for example purposes), instead use the following:
First build your array into a variable, call it myArray..
then initialize your adapter like this:
mAdapter = new ArrayAdapter<String>(this, layoutId, myArray);
Now make sure that myArray is a class variable so you can reference it from anywhere else in the class.. Of course if you need access to this from another class you'd want to make a getter for it... Then you can easily iterate over the array to see if the value selected is in the array.. You'll have the whole set of data there instead of trying to get it from the adapter.
Here is a good example on using a validator for a similar looking use case:
Android, Autocomplettextview, force text to be from the entry list
In my case, I have address that can be set either by autocomplete or clicking on the map. If user click on the map, editText text should be set to address selected from the map, and in that case filtering should be disable temporarily.
I code like this:
public void OnAddressFound(String address) {
// Temporary disable autocomplete
editTextSearch.setAdapter(null);
editTextSearch.setText(address);
// Enable autocomplete again
setAutoCompleteAdapter();
}
where setAutoCompleteAdapter() is called during onCreate, and again in temporary disable/enable filter:
void setAutoCompleteAdapter() {
PlacesAutoCompleteAdapter adapter = new PlacesAutoCompleteAdapter(this, R.layout.item_autocomplete_map_search, autoCompleteList);
editTextSearch.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
Hope its help you also.
I found the solution, kinnda tricky but its works
after i looking inside of the source, i found that th treshold variable used as filtering validation, here we just need to set the treshold to maximum int so filtering never perform.
threshold = Integer.MAX_VALUE
or
setThreshold(Integer.MAX_VALUE)