kanchan
kanchan

Reputation: 77

Spinner OnItemSelectedListener Issue

I have problem with spinner control. I am trying to set spinner items dynamically. Initially I have one item in spinner.

When I try to register the spinner.setOnItemSelect Listener, it immediately call onItemSelected method of it. However I don't want to call this method as soon as my activity get started.

So for this I put a following condition.

public class SpinnerActivity extends Activity implements OnItemSelectedListener {

Spinner spinner;

String[] str_arr = {"aaaaaaaa"};

private int mSpinnerCount=0;

private int mSpinnerInitializedCount=0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner);

    spinner = (Spinner) findViewById(R.id.spinner1);

    spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str_arr));

    spinner.setOnItemSelectedListener(this);        

    mSpinnerCount=1;

}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {

    if (mSpinnerInitializedCount < mSpinnerCount) {
        mSpinnerInitializedCount++;
    }

    else {

        Intent intent = new Intent(this, NextActivity.class);
        startActivity(intent);
    }

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
  }

But when I try to select an item on spinner it gives following warning in logcat,

09-03 13:02:02.528: W/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@450fafb8

I get the idea that until and unless Item of spinner won't change this method won't be called.

But I have one value in spinner, so how to get the focus, any idea?

Upvotes: 1

Views: 5308

Answers (4)

user1496130
user1496130

Reputation:

Try like this

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {

    if (position != 0) {
       //put your actions here
    }

    else {
      // nothing here or toast 
    }

}

Upvotes: 1

Harpreet
Harpreet

Reputation: 3070

Try this to what i said in comment...

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {

    if (position > 0) {
       //Your actions
    }

    else {
      // Nothing or can show a toast to say user to select a value... 
    }

}

Upvotes: 2

Archie.bpgc
Archie.bpgc

Reputation: 24012

You get this warning when you try to open already opened window, or try to do something like onFocus on already focused view.

Here you already have the item selected in the Spinner

Upvotes: 0

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

I think the below code are not right because you implements OnItemSelectedListener

spinner.setOnItemSelectedListener(this);

Upvotes: 0

Related Questions