I am developing an android app, which asks security questions after Sign Up for forgot password purpose, which has 10 questions in total. User can select any 3.
I have 3 Spinner for 3 question. Once user select the question from first spinner, second and third spinner should not have them in their list. Please help me to disable or remove that from the list.
screen shot of the activity
First, set a boolean check if it is the first time a spinner is selected. Store the selected item, so that you can add them on question change later.
Boolean ifFirstCheck = true;
String storeItem = "";
Then, Use below code:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(!ifFirstCheck){
listSp2.add(storeItem);
sp2adapter.notifyDataSetChanged();
listSp3.add(storeItem);
sp3adapter.notifyDataSetChanged();
}
String selectedItem = spinner1.getSelectedItem().toString();
listSp2.remove(selectedItem) // Get selected value from spinner1 and remove thar item from spinner2
sp2adapter.notifyDataSetChanged(); // Notify adapter of spinner2 to that dataset has been changed
listSp3.remove(selectedItem)
sp3adapter.notifyDataSetChanged();
storeItem = selectedItem;
ifFirstCheck = false;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Do as above for rest of the spinners.Hope this helps.
I assume you are using an Adapter for the Spinners. If you use an ArrayAdapter and each adapter holds the same list of items, you can just remove the selected items from the list and notify the adapters to update.
Related
I am new to android and i want to make 3 spinner, 1 is to work, 2 is stain and 3rd is price. The data should come from server through JSON. If i select Metier example plumber then by selection of plumber i should get the list of tasks related to the plumber. . Price should get the price. The spinner should change according to 1st spinner.
All you have to do that give a network call on first spinner selection
for example:
if plumber is selected in first spinner post this string to server and get the data regarding this string "plumber"
after you get the data populate the data with second and third spinner
Hope its help :)
final Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String items = spinner.getSelectedItem().toString();
//Here give a network call
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
I have a spinner and I want to get the last selected value of it when I click on the spinner (basically just before the items in the list are being shown).
How can I achieve this?
Update:
Example: There are a dynamic amount of spinners. User clicks in spinner 1 and selects a value, let's say "house". Then clicks somewhere else. Then clicks in spinner 1 again. What I now need is to return the value "house" that was previously was selected before the user selects a different value i.e. "car" in that spinner. I can not use the local storage to save that value beforehand because it's going to be a dynamic amount of spinners to be added.
And yes, I did in fact read the documentation at http://developer.android.com/guide/topics/ui/controls/spinner.html already but what I need isn't explained there.
Maybe you can declare a String array and then use it to storage the items values. Showing this in a TextView is a way to see if it works.
Something like this:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
selection.setText(items[arg2]);
}
I already have faced a similar situation, but I don't remember exactly how I solved it. Tell me if it gives a better idea to you, I'm new participating here.
Spinner mySpinner=(Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
When the user selects an item from the drop-down, the Spinner object
receives an on-item-selected event.
To define the selection event handler for a spinner, implement the
AdapterView.OnItemSelectedListener interface and the corresponding
onItemSelected() callback method. For example, here's an
implementation of the interface in an Activity:
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
The AdapterView.OnItemSelectedListener requires the onItemSelected()
and onNothingSelected() callback methods.
Then you need to specify the interface implementation by calling
setOnItemSelectedListener():
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
IN YOUR CASE:
TO se if the user clicked on the spinner, do this:
if (spin.getSelectedItemPosition() < 0) {
Spinner mySpinner=(Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
}
This means user clicked the spinner, but not any items of it.
EDIT AGAIN
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
> ...
>
> public void onItemSelected(AdapterView<?> parent, View view,
> int pos, long id) {
> parent.getItemAtPosition(pos) //THIS GETS YOU THAT VALUE THAT THE USER CLICKED ON DYNAMICALLY
> }
>
> public void onNothingSelected(AdapterView<?> parent) {
> // Another interface callback
> }
> }
http://developer.android.com/guide/topics/ui/controls/spinner.html
Solution is to add a TextView widget with each added Spinner. Set the TextView to "gone" (not visible). When the spinner changes set the value to that TextView. This way you can store the last selected spinner value in the TextView. When the user clicks on the spinner and changes the value you can still access the previously selected/unselected value from the spinner through the TextView widget.
// Use this TextView to temporarily store the spinner value
final TextView hiddenTextView = new TextView(getContext());
hiddenTextView.setVisibility(View.GONE);
final Spinner spinner = new Spinner(getContext());
spinner.setPrompt(getString(R.string.email_other));
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), R.array.email_types, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.d(TAG, "TextView Value before spinner change: " + hiddenTextView.getText());
String spinnerValue = String.valueOf(spinner.getSelectedItem());
hiddenTextView.setText(spinnerValue);
Log.d(TAG, "TextView Value after spinner change: " + hiddenTextView.getText());
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
I want to exectute Async task on selecting a spinner item and add some data to another spinner. It works well if i select a item. But it automatically executes AsyncTask even when not selecting. This is what i have tried
district
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
ssservice));
// Spinner on item click listener
district
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
// ed_spinner = district.getSelectedItem().toString();
new NetCheck2().execute();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
How to execute Async Task only after I click on spinner item. Please help me.
The spinner is badly designed. Unfortunately when you call spinner.setOnItemSelectedListener it automatically calls onItemSelected. This is not a problem with your code, the spinner is designed that way. I know its bad, but this is how it works.
Workaround
If suppose your spinner has 4 entries, add one more entry at top i.e. 0th position and now when spinner is created onItemClicked is called and it will have position 0. So just put a if condition that
if (!(position == 0)){//do your stuff}
Here in this code i have added pincode numbers from 600000 to 600113 in the spinner but i want the first position to be just empty. When the user clicks only then the item should be shown. Please see my code -
final String[] myarray=new String[114];
for(int i=0;i<114;i++)
{
myarray[i]=String.valueOf(a);
a++;
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,myarray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
addnum_spinner.setAdapter(adapter);
lv.setOnItemClickListener(itemClickedListener);
//Spinner click
addnum_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
String value = myarray[position];
//
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
add this property to your spinner in xml and try if that works
android:prompt=""
I don't know if this is the right way but what about just adding the empty string before the for loop as the first item and then you just have to handle the user clicking on the empty item in your Listener. Something like
final String[] myarray=new String[115];
myarray[0] = ""
for(int i=1;i<115;i++)
{
myarray[i]=String.valueOf(a);
a++;
}
I know this question is old, but I'm adding a solution. I found this out on accident, as I DIDN'T want a blank default value, but there was one.
If using an ArrayAdapter, I found that attaching the adapter to an empty ArrayList, and THEN filling it left a blank initial value.
Filling the ArrayList first, then attaching the ArrayAdapter removed the blank default value.
Obviously, if not using an ArrayAdapter, using the prompt, or setting the first Spinner item as <item></item> with nothing in between in the XML should work.
I have a spinner with a custom adapter displaying objects from a database.
When the object list changed I create a new adapter with the List and apply it on the spinner. Afterwards the first item is selected, so I tried this:
// 5th item selected
int pos = spinner.getSelectedItemPosition();
spinner.setAdapter(newAdapter);
// 0th item selected
spinner.setSelectedItem(pos);
// 5th item is selected
But the GUI does still show the first item?
spinner.invalidate() did not help.
Is this the correct way to achieve what I want? I really could not find any information on this behavior.
Solved: I guess the main problem was the custom spinner adapter. This works fine now
if (spinner.getCount() > 0) {
pos = spinner.getSelectedItemPosition();
}
MySpinnerAdapter adapter = new MySpinnerAdapter(context, myNewObjects);
spinner.setAdapter(adapter);
spinner.setSelection(pos); // needed
adapter.notifyDataSetChanged();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
spinner.setSelectedItem(5);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Did u tried notifydatasetChanged() or notifydatasetInvalidate() method of adapter.
newAdapter.notifydatasetChanged()