I have two spinner in my system. Now I have to change the selected value of the 2nd spinner depending on the first spinner value. As soon as the user will change the 1st spinner value, the 2nd spinner value will set automatically depending upon the 1st spinner's selected value. How to implement this?
From the Hello Spinner tutorial:
Now create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner. Here's what this class should look like:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The former is called when an item from the AdapterView is selected, in which case, a short Toast message displays the selected text; and the latter is called when a selection disappears from the AdapterView, which doesn't happen in this case, so it's ignored.
Now the MyOnItemSelectedListener needs to be applied to the Spinner. Go back to the onCreate() method and add the following line to the end:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
In other words, you need to create an OnItemSelectedListener that modifies the value of the second spinner, and attach it to the first spinner.
You have to put the condition on onItemSelected of very first spinner. By this example you can get value of 2nd spinner depending on 1st spinner:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(arg0.equals(spin0)){
spin1.setClickable(true);
if(spin0.getSelectedItem().equals("India"))
{
ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s1);
}
else if(spin0.getSelectedItem().equals("Pakistan"))
{
ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s2);
}
else if(spin0.getSelectedItem().equals("China"))
{
ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s3);
}
}
}
You should define the onItemSelected() separately for each spinner. Otherwise the code gets executed if anything is selected from either spinners.
newCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String selCat = newCategory.getItemAtPosition(arg2).toString();
if (selCat != "New")
{
loadSpinnerData(topic);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
newTopic.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
loadSpinnerData()
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Related
Hello friends i have spinner as following
When i click on any item click event than it should be set like following
it means when i select Afghnistan at that time it should be set value as AF and rest of country value remain as it is my code is as follow
On Spinner item click
mSpinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
mStringCountryID=mArrayListCountryDatas.get(position).getId();
mSpinnerCountry.setPrompt(mArrayListCountryDatas.get(position).getIso_alpha()+"
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
When i run above code it is not set value any idea how can i make it possible ?
mSpinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int mStringCountryID = s1.getSelectedItemPosition(); Toast.makeText(getBaseContext(),
"You have selected item : " + presidents[mStringCountryID],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this... variable mStringCountryID will hold your specific complete value do what ever you want to do with this value.
I'm using a spinner in order to select a certain value, and in the activity that contains the spinnr, I've implemented OnItemSelectedListener.
in the OnCreate() method, I've defined:
spinner1.setOnItemSelectedListener(this);
but it doesn't get to the method onItemSelected(), my guess is that I'm not sending the right variable ("this").
what should I send to setOnItemSelectedListener in order for this to get to the method?
Thank You!
activity that contains the spinnr, I've implemented
OnItemSelectedListener
If OnItemSelectedListener listener is implemented in Activity then override onItemSelected in Activity :
#Override
public void onItemSelected(AdapterView<?> parent,
View selectedItemView, int position, long id) {
// your code here
}
Try this.
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Do something
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Also you can look at this. http://developer.android.com/guide/topics/ui/controls/spinner.html
I have a list view and using two listeners - OnItemClicked and OnItemSelected. The clicked listener is working properly but onItemSelected Listener is not getting invoked. I need OnItemSelected listener because sometimes selected is set pragmatically.
allClues.post(new Runnable() {
public void run() {
listView.setSelection(ind);
}
});
I am expecting that when selection is set its OnSelection listener will be invoked. But its not happening.
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
iSelected = arg2 ;
Log.e("listargs", (String.valueOf(arg1)) + " " + String.valueOf(arg3));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
What could be wrong here ?
OnItemSelectedListener is used for the Spinner not for the Listview For Listview you have to use OnItemClickListener
You have to use OnItemClickListener like this
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int i,
long j) {
// do ur code
}
});
I have a spinner control with setOnItemSelectedListener in my app, when I select an item of the spinner, the event onItemSelected will be launched.
Now I hope to click a button to launch the onItemSelected event, how can I do? Thanks!
spinnerFolder.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
//Do Business Logic
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Just call the following from your button listener:
spinnerFolder.getOnItemSelectedListener().onItemSelected(spinnerFolder, spinnerFolder.getSelectedView(), spinnerFolder.getSelectedItemPosition(), spinnderFolder.getSelectedItemId());
That's all :-)
The third parameter (int arg2) of onItemSelected for your spinner is the position, so you can get the current selection by
String selection = (String) mSpinnerAdapter.getItem(position);
or use mSpinner.getItemAtPosition(position) if you don't have a custom adapter.
Store the current selection somewhere and pick it up in the onClickListener for your button.
When you populate the spinner with some data, you will have some sort of list objects (Strings, custom object, whatever) in the SpinnerAdapter, you'll keep a reference to that list, let's call it: private List<Object> dataList = ...
First of all I would create a method that would handle the specific data object:
protected void doBusinessLogic(Object myObj) {
// do the things that make you happy
}
Then call this from listener as:
spinnerFolder.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// args2 is the position from backed data list
doBusinessLogic(dataList.get(args2));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Then from button listener you would call above method again, but the object index you would get is from spinnerFolder.getSelectedItemPosition();:
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int dataIndex = spinnerFolder.getSelectedItemPosition();
doBusinessLogic(dataList.get(index));
}
});
Is it possible to add selected spinner item in a table (which is generating dynamically) in textview on button click.
Generally I use this code:
spinner=new String[5];
spinner[0]="1";
spinner[1]="2";
spinner[2]="3";
Spinner spn = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, spinner);
spn.setAdapter(adapter);
but what to do when to add the selected value in table?
you need to listen to the Spinner's OnItemSelected event. And don't forget to set the layout for the drop-down items! So in order:
Set the Adapter like you did correctly: spn.setAdapter(adapter);
Set the drop-down item view: spn.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Hook on the OnItemSelected event:
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View arg1,
int position, long arg3) {
// gets called when the user selects an item
TextView txt = (TextView)findViewById(R.id.your_textview_id);
txt.setText((String)adapter.getItemAtPosition(position));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// what to do when nothing is selected
}
});
That's it...
After the Spinner is populated get the Selected value in Spinner by using OnItemSelectedListener.
Here is the code to get the Selected value in Spinner :
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
selectedspinnervalue =spinner.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
In which selectedspinnervalue contains the selected value..
Then you add the value in Table..