【问题标题】:Android Instrumentation Test : ListView content not loadingAndroid Instrumentation 测试:ListView 内容未加载
【发布时间】:2012-06-25 09:15:07
【问题描述】:

我正在尝试为我的 android 项目编写测试类,但在填充列表视图时遇到了一些问题。

我的活动使用 Intents 提供的值对应用程序数据库执行请求:

    final String[] projection = {
             ...
            };

    final String[] selectionArgs={
            String.valueOf(intent_param_1),
            String.valueOf(intent_param_2)
    };
    final String selection = //.... ;

    vAdapter = new CustomCursorAdapter(this, R.layout.da_row, null, from, to,0);

    mListView = new LoaderUtils(vAdapter, projection, selection, selectionArgs, this, uri, null).populateListView(mListView);

LoaderUtils 类实现了LoaderCallbacks,方法populateListView 使用了一个CursorLoader 将DB 数据加载到适配器并绑定到我的ListView。 它在运行我的应用程序时工作正常,但是当我尝试实现测试用例时,我遇到了一些问题。

@Override
  protected void setUp() throws Exception {
    super.setUp();

    Intent i = new Intent();
    i.putExtra("intent_param_1", 1);
    i.putExtra("intent_param_2", 1);
    setActivityIntent(i);

    mActivity = getActivity();
    listeActivites = (ListView)mActivity.findViewById(R.id.listview);
    adapter = (CustomCursorAdapter) listeActivites.getAdapter();
  }

public void testInit(){
    Cursor c = adapter.getCursor();
            //...
    }

当我执行我的 testInit 时,适配器返回的光标是空的,而当我使用模拟器“真正”运行我的应用程序时它不是。

问题可能是我的 Intent 初始化错误,或者测试用例不允许使用 Cursor Loaders?因此,如果有人能指出我做错了什么,将不胜感激。谢谢!

【问题讨论】:

    标签: android listview testing junit android-cursorloader


    【解决方案1】:

    原因是CursorLoader 还没有完成。如果你增加一秒钟的睡眠时间(不是正确的方式),它就会起作用。

    我目前也在寻找如何最好地使用 CursorLoaders 测试活动。既然不想加睡眠代码……一定有更好的办法。

    目前正在考虑添加一个我可以在测试中等待的侦听器...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 2021-03-29
      相关资源
      最近更新 更多