Spinner OnItemSelectedListener Issue

蓝咒 提交于 2020-01-05 07:08:10

问题


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?


回答1:


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 
    }

}



回答2:


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... 
    }

}



回答3:


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

spinner.setOnItemSelectedListener(this);



回答4:


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



来源:https://stackoverflow.com/questions/12243834/spinner-onitemselectedlistener-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!