Robolectric test ListView does not update after changing value in adapter

I created a layout with a ListView that updates the values ​​in it, depending on which of the 2 buttons pressed is pressed. Pressing the button labeled Odds clears all values ​​and adds the odd numbers to 20. Pressing the evens button does the same, except for the even numbers. This works great when emulating or checking on a real device. Below is the activity code.

 public class ListViewActivity extends Activity { private ListView mainListView; private ArrayAdapter<String> listAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.robolectric_test_main); mainListView = (ListView) findViewById(R.id.mainListView); ArrayList<String> numbersList = new ArrayList<String>(); for (int i = 1; i < 21; i++) { numbersList.add(String.valueOf(i)); } listAdapter = new ArrayAdapter<String>(this, R.layout.robolectric_listview_row, numbersList); mainListView.setAdapter(listAdapter); } public void onOddsClicked(View view) { listAdapter.clear(); for (int i = 1; i < 21; i += 2) { listAdapter.add(String.valueOf(i)); } } public void onEvensClicked(View view) { listAdapter.clear(); for (int i = 2; i < 21; i += 2) { listAdapter.add(String.valueOf(i)); } } } 

However, when you try to test it using Robolectric, the ListView never updated, even if the adapter has value modifications. Here is my test code.

 @RunWith(RobolectricTestRunner.class) public class ListViewActivityTest { private ListViewActivity listViewActivity; private Button oddsButton; private Button evensButton; private ListView numbersList; @Before public void setUp() throws Exception { listViewActivity = Robolectric.buildActivity(ListViewActivity.class).create().start().resume().visible().get(); assignFields(); } private void assignFields() { oddsButton = (Button) listViewActivity.findViewById(R.id.oddsButton); evensButton = (Button) listViewActivity.findViewById(R.id.evensButton); numbersList = (ListView) listViewActivity.findViewById(R.id.mainListView); } @Test public void odds() throws Exception { // check all numbers are odd Robolectric.clickOn(oddsButton); Robolectric.runUiThreadTasksIncludingDelayedTasks(); for (int i = 0; i < numbersList.getChildCount(); i++) { TextView number = (TextView) numbersList.getChildAt(i); String numberString = number.getText().toString(); int parsedInt = Integer.parseInt(numberString); assertTrue((parsedInt % 2) == 1); } } @Test public void evens() throws Exception { // check all numbers are even Robolectric.clickOn(evensButton); Robolectric.runUiThreadTasksIncludingDelayedTasks(); for (int i = 0; i < numbersList.getChildCount(); i++) { TextView number = (TextView) numbersList.getChildAt(i); String numberString = number.getText().toString(); int parsedInt = Integer.parseInt(numberString); assertTrue((parsedInt % 2) == 0); } } } 

Could you tell me what I need to do to start updating the ListView via Robolectric.

+8
android android-listview listview robolectric
source share
2 answers

In robolectric, you must call ShadowListView.populateItems() to get adapter changes

Robolectric 3.0

 ShadowListView shadowListView = Shadows.shadowOf(mListView); shadowListView.populateItems(); 

Robolectric 2.0

 ShadowListView shadowListView = Robolectric.shadowOf(listView); shadowListView.populateItems(); 
+10
source share

Just update Robolectric 3.0 answer

  ShadowListView shadowListView = Shadows.shadowOf(mListView); shadowListView.populateItems(); 
+4
source share

All Articles