Spinner setOnItemSelectedListener Doesn't Fire - android

I have a view object that contains a edittext, an invisible spinner, and a button. I load the view via the LayoutInflater and it shows up and everything works fine. I hit the button, the spinner list shows up and I select the item that I want. My problem is that the setOnItemSelectedListener for the spinner doesn't fire so I can't set the edittext to the selected value.
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, listItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spn = (Spinner)sEdit.findViewWithTag("spn"+fieldID);
final String spnHolder = "spn"+fieldID;
spn.setAdapter(adapter);
ImageButton bSpn = (ImageButton)sEdit.findViewWithTag("btn"+fieldID);
bSpn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
curVw = (EditText)sEdit.findViewWithTag(v.getTag().toString().split("btn")[1]);
((Spinner)sEdit.findViewWithTag(spnHolder)).performClick();
}
});
try {
spn.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
curVw.setText(((TextView)arg1).getText().toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
catch (Exception ex)
{
ex.printStackTrace();
}
I added a Try...Catch to see if it was just failing past that point, but it is never called.

Instead of using SetOnclickListener please use:
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
//implements methods and perform your action. This will works definately
};

Related

Display EditText items on Spinner selection

I have a group of items in spinner and on their click I want to show the respective text in EditText . How I can achieve this. I thought of using switch but it doesnot work for strings. I want someone to tell me right approach for doing this.
I want the EditText array(mysuburb) to respond accordingly to the Spinner items(mystate) click .
Code:-
String[] mysuburb =new String[]{"sub1" ,"sub2","sub3","sub4","sub5","sub6"};
String[] mystate= new String[]{"NSW","Victoria","Qld","NT","WA","SA"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.listrow, mystate);
// LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
// TODO Auto-generated method stub
sstate = state.getSelectedItem().toString();
/* String sub= state.getItemAtPosition(0).toString();
if(sub=="sub1")
suburb.setText("sub1") ; */
suburb.setText(arg0.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,int i, long l) {
sstate = state.getSelectedItem().toString();
suburb.setText(sstate);
}
}

How can I click a button to launch an ItemSelected event of spinner control?

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

Adding OnItemSelectedListener to Spinner

I have a button and a spinner (originally hidden). When user presses a button, spinner gets populated with items and becomes visible. Now I would like to add OnItemSelectedListener to the spinner. and I have tried many tutorials with no luck.
This is my OnCreate function
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button load_routes = (Button)findViewById(R.id.load_routes);
Spinner routes = (Spinner)findViewById(R.id.routes_list);
load_routes.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
load_routes(v);
}
});
routes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id)
{
Log.v("routes", "route selected");
}
public void onNothingSelected(AdapterView<?> arg0)
{
Log.v("routes", "nothing selected");
}
});
}
This is my load_routes function
private void load_routes(View v)
{
Spinner routes = (Spinner)findViewById(R.id.routes_list);
List<String> routes_list = RouteParser.get_routes();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes_list);
routes.setAdapter(adapter);
TableRow list_of_routes_row = (TableRow)findViewById(R.id.list_of_routes_row);
list_of_routes_row.setVisibility(View.VISIBLE);
}
This set up does not work. The only way I got this to work is when I setup my listener as routes.setOnItemSelectedListener(this) Then I implement OnItemSelectedListener and include the functions neccessary. But I have multiple spinners and need to create separate listeners for different spinner. Any help will be appreciated. Thanks!
final String[] s2 = getResources().getStringArray(R.array.capteur_size);
final EditText ed = (EditText) findViewById(R.id.editTextCoC);
spinnerCoC = (Spinner) findViewById(R.id.spinnerCoC);
spinnerCoC.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ed.setText(s2[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Declare your Spinner as field instantiate the listener once you do findViewById and use it wherever you want.

Related spinners Android

I'm begginer in android. I'm working on a project. But i get very difficult to do two spinners related to each others. Actually one spinner for the country and another for the city. Instead of the country that is chosen the second spinner will show the cities.
I'v used "OnItemSelectedListener" but the " ArrayAdapter.createFromResourc​e " can't be used inside OnItemSelectedListener.
I've tried a lot of other ways but still none of them working.
Can anybody help me Please???
(P.S. I have read and tried the other posts about this topic but it still doesn't work )
This is the code:
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
int spinnerId = spinner.getSelectedItemPosition();
if (spinnerId==0){
adaptert = ArrayAdapter.createFromResource(
this, R.array.tirana, android.R.layout.simple_spinner_item);
adaptert.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
else if (spinnerId==1) {
adaptert = ArrayAdapter.createFromResource(
this, R.array.durres, android.R.layout.simple_spinner_item);
adaptert.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
spinnert.setAdapter(adaptert);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
public class AdapterViewImplementation extends Activity implements OnItemSelectedListener{
Spinner sp1; // One Spinner
Spinner sp2; // Another Spinner
ArrayAdapter stateAdapter; // Adapter for state
ArrayAdapter cityAdapter; // Adapter for city
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp1 = (Spinner)findViewById(R.id.Spinner01);
sp2 = (Spinner)findViewById(R.id.Spinner02);
stateAdapter = ArrayAdapter.createFromResource(AdapterViewImplementation.this,
R.array.state, android.R.layout.simple_spinner_item);
stateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(stateAdapter);
sp1.setOnItemSelectedListener(AdapterViewImplementation.this);
cityAdapter = ArrayAdapter.createFromResource(AdapterViewImplementation.this,
R.array.city, android.R.layout.simple_spinner_item);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(cityAdapter);
sp2.setOnItemSelectedListener(AdapterViewImplementation.this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(arg0 == sp1){
sp2.setSelection(arg2);
}else{
sp1.setSelection(arg2);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

Spinner on value Change

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

Categories

Resources