티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How to keep onItemSelected from firing off on a newly instantiated Spinner?

어떻게 하면 새롭게 인스턴스화된 Spinner에서 onItemSelected가 실행되지 않게 유지할 수 있을까요?

 문제 내용 

I've thought of some less than elegant ways to solve this, but I know I must be missing something.

이 문제를 해결하는 덜 우아한 방법들을 고민해 보았지만, 뭔가 빠뜨린 것이 분명하다고 생각합니다.

 

My onItemSelected fires off immediately without any interaction with the user, and this is undesired behavior. I wish for the UI to wait until the user selects something before it does anything.

사용자와 상호작용하지 않고 즉시 onItemSelected가 실행되어, 이는 원하지 않는 동작입니다. 사용자가 무언가를 선택할 때까지 UI가 대기하도록 하려고 합니다.

 

I even tried setting up the listener in the onResume(), hoping that would help, but it doesn't.

도움이 되길 바라며 onResume()에서 리스너를 설정하려고 시도했지만 도움이 되지 않았습니다.

 

How can I stop this from firing off before the user can touch the control?

사용자가 컨트롤을 터치하기 전에 이벤트가 발생하는 것을 방지하려면 어떻게 해야 할까요?
public class CMSHome extends Activity { 

private Spinner spinner;

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

    // Heres my spinner ///////////////////////////////////////////
    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.pm_list, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    };

public void onResume() {
    super.onResume();
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}

    public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {

     Intent i = new Intent(CMSHome.this, ListProjects.class);
     i.putExtra("bEmpID", parent.getItemAtPosition(pos).toString());
        startActivity(i);

        Toast.makeText(parent.getContext(), "The pm is " +
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}
}

 

 

 높은 점수를 받은 Solution 

The use of Runnables is completely incorrect.

Runnables의 사용법이 완전히 잘못되었습니다.

 

Use setSelection(position, false); in the initial selection before setOnItemSelectedListener(listener)

setOnItemSelectedListener(listener)를 호출하기 전에 초기 선택에서 setSelection(position, false)를 사용하세요.

 

This way you set your selection with no animation which causes the on item selected listener to be called. But the listener is null so nothing is run. Then your listener is assigned.

이렇게 하면 초기 선택에서 setOnItemSelectedListener(listener) 이전에 setSelection(position, false)을 사용합니다. 이렇게 하면 애니메이션 없이 선택이 설정되며, 이로 인해 on item selected 리스너가 호출됩니다. 그러나 리스너가 null이기 때문에 아무것도 실행되지 않습니다. 그런 다음 리스너가 할당됩니다.

 

So follow this exact sequence:

그래서 이 정확한 순서를 따르세요:
Spinner s = (Spinner)Util.findViewById(view, R.id.sound, R.id.spinner);
s.setAdapter(adapter);
s.setSelection(position, false);
s.setOnItemSelectedListener(listener);

 

 

 가장 최근 달린 Solution 

This will happen if you are making selection in code as;

이는 코드로 선택을 만드는 경우 발생합니다.
   mSpinner.setSelection(0);

 

Instead of above statement use

위의 문장 대신에 다음 문장을 사용하세요.
   mSpinner.setSelection(0,false);//just simply do not animate it.

 

Edit: This method doesn't work for Mi Android Version Mi UI.

수정: 이 방법은 Xiaomi Android 버전인 MIUI에서는 작동하지 않습니다.

 

 

 

출처 : https://stackoverflow.com/questions/2562248/how-to-keep-onitemselected-from-firing-off-on-a-newly-instantiated-spinner

반응형
댓글
공지사항
최근에 올라온 글