Android:在ListView中获取第i个TextView

时间:2010-12-26 12:12:16

标签: android unit-testing listview

我正在尝试编写一个小应用程序和相关的单元测试。 我有一个ListView绑定到SimpleCursorAdapter从SQL表读取数据。

Activity#onCreate()方法是:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    dbHelper = new DatabaseHelper(this);
    SQLiteDatabase dbRead = dbHelper.getReadableDatabase();

    String[] columns={BaseColumns._ID, ENTRY_VALUE};
    cursor = dbRead.query(ENTRIES_TABLENAME, columns, null, null, null, null, null);

    String[] from = {"value"};
    int[] to = {R.id.value};
    adapter = new SimpleCursorAdapter(this, R.layout.list_entry, cursor, from, to);
    setListAdapter(adapter);
}

我在单元测试中的测试是:

     @UiThreadTest
     public void testTheElementInsideTheDBisDisplayedInTheList() {
          String entryValue = "clipboard entry 1";
          DatabaseHelper dbHelper = new DatabaseHelper(cmActivity);
          Cursor beforeCursor = selectAllEntries(dbHelper);

          // The table, at the begining of the test, is empty, I control that
          assertEquals(0, beforeCursor.getCount());

          // I insert the new value in the table  
          insertEntry(dbHelper, entryValue);

          // and I control that is really inside the table now  
          Cursor afterCursor = selectAllEntries(dbHelper);
          assertEquals(1, afterCursor.getCount());

          // This method calls the method "requery()" on the cursor associate
          // to the listView's adapter to update the list view  
          cmActivity.updateList();

          // I control that the listView is updated
          assertEquals(1, entryList.getCount());
          // Now I try to retrive the only child inside the list view
          // to extract the text inside it and to compare this text with
          // the value inserted into the DB table.
          TextView entryView = (TextView) entryList.getChildAt(0);
          String viewText = entryView.getText().toString();
          assertEquals(entryValue, viewText);
     }

我的问题出在第三行:

  TextView entryView = (TextView) entryList.getChildAt(0);

我sude getChildAt()获取ListView的第一个TextView子级。但是此方法返回null,因此测试会获得NullPointerException。

也许getChildAt()不是从ListView获取View子对象的正确方法,哪个是正确的?

我从文档中看到该方法适用于GroupView,我没有使用它们,我是否需要配置默认的GroupView并将所有条目放入其中?这样,getChildAt(0)会工作吗?这是设置ListView的正确方法吗?

谢谢,再见 安德烈

正如Vivek所说,我在这里发布了main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView  
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />
    <TextView android:id="@android:id/empty"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Empty set"
         />
</LinearLayout>

你可以看到非常非常基本的。另外列表条目非常简单:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/value"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

1 个答案:

答案 0 :(得分:6)

我怀疑在调用getChildAt()方法时是否填充了列表。因此,请调用 getChildCount()方法,并查看是否已填充列表。并在此处回发输出。

修改

现在我明白了这个问题。 ListView.getCount()方法返回列表中填充的项目数。 ListView.getChildCount()方法或 ListView.getChildAt()方法将在此处返回0,因为这些方法仅在视图对用户可见时才返回值。只有在生成文本视图后才能使用 getChildAt()方法。即如果您使用listview的 OnItemClick 方法或任何listview侦听器实现中的方法,您将获得所需的输出。无论如何,需要在此方法中获取对textviews的引用是什么?