how to disable spinner click? setclickable(false) not working - android

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());
}

Related

SwipeMenuListView new Row added is showing First time data

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 .

Spinner not showing selected item

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.

Set Spinner to Unselected

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);

Is there a way to get notified when a spinner is activated?

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!

Problems with creating spinner

In my app I'm developing, I have a spinner in an alert dialog box.
The spinner works fine, but when I added the following lines to add the array to the spinner, my app crashing a few seconds after starting up:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.newFileTypeArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
newFileType.setAdapter(adapter);
What am I doing wrong?
These Android spinners seem to be a bit complicated as well, I don't think I'll be able to remember how to make them without referencing to the Android docs.
Problem solved.
I realised that the following line:
final Spinner newfiletypespinner = (Spinner) findViewById(R.id.newfiletypespinner);
I had to change to:
final Spinner newfiletypespinner = (Spinner) newFileDialogInflated.findViewById(R.id.newfiletypespinner);
With "newFileDialogInflated" being the previously inflated view so I could have a custom AlertDialog view:
final View newFileDialogInflated = View.inflate(this, R.layout.newfileview, null);
But thanks for the help!
Hard to say for sure whether you've omitted it from your snippet or omitted it from your code, but do you initialize newFileType as a spinner?
Spinner newFileType = (Spinner)findViewById(R.id.newFileTypeSpinner);
or similar? If you're trying to set its adapter before initializing it, that would explain it.

Categories

Resources