How to select the first counter element in a test

I have tests where I test OnItemSelectedListener on Spinner. It works great when testing elements> 0. But it looks like I can't check the first element.

My current implementation, which works if I select items with index> 0, looks like this.

final Addpointer addPointer = getActivity(); addPointer.runOnUiThread(new Runnable() { @Override public void run() { EditText address = (EditText) addPointer.findViewById(R.id.address); address.setText("a"); Spinner spinner = (Spinner) addPointer.findViewById(R.id.intOrHex); spinner.setSelection(0); View view = (View) spinner.getChildAt(0); long id = spinner.getAdapter().getItemId(0); spinner.performItemClick(view, 0, id); } }); 

What do I need to do to get the test to "select" the first element?

Thanks in advance

Rolling

Answer: 1) Rahul garg about setting up "animate" was the key to solving the problem. 2) But you cannot activate onSelectionChanged if the selection has not actually changed (0 was the initial state, so I had to set it to a value before I return it to zero.

+6
source share
3 answers

Using

 spinner.setSelection(0,true); 

The second parameter will actually animate the selection to index 0.

+7
source

more "transparent" way:

  Spinner.setSelection(Adapter.NO_SELECTION, false);** 

* second argument for selection animation

** this should be called after:

  Spinner.setAdapter(...); 

or any methods that include Spinner.setSelection () , for example:

  Adapter.notifyDataSetChanged(); 

& for OnItemSelectedListener () do not run

+6
source

See the SpinnerTest sample application in the Developer's Guide. It actually sends keystrokes to activity.

0
source

Source: https://habr.com/ru/post/909924/


All Articles