作者:依然-狠幸福 | 来源:互联网 | 2023-05-22 10:10
我创建了一个布局,ListView
其中的更新值取决于按下的2个给定按钮中的哪一个.按下标有赔率的按钮将清除所有值,并将奇数加到20.除均数外,按下均匀值相同.这在真实设备上模拟或尝试时工作正常.以下是活动代码.
public class ListViewActivity extends Activity {
private ListView mainListView;
private ArrayAdapter listAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.robolectric_test_main);
mainListView = (ListView) findViewById(R.id.mainListView);
ArrayList numbersList = new ArrayList();
for (int i = 1; i <21; i++) {
numbersList.add(String.valueOf(i));
}
listAdapter = new ArrayAdapter(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));
}
}
}
但是,当尝试使用Robolectric进行测试时,ListView
即使适配器具有值修改,也永远不会更新.这是我的测试代码.
@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
你能告诉我我需要做什么才能触发ListView
通过Robolectric 的更新.
1> nenick..:
在robolectric中,您必须调用ShadowListView.populateItems()
以获取适配器更改
Robolectric 3.0
ShadowListView shadowListView = Shadows.shadowOf(mListView);
shadowListView.populateItems();
Robolectric 2.0
ShadowListView shadowListView = Robolectric.shadowOf(listView);
shadowListView.populateItems();