开发者

Android: get the i-th TextView inside a ListView

开发者 https://www.devze.com 2023-02-01 19:29 出处:网络
I\'m try to write a little application and the releated unit tests. I have a Li开发者_开发问答stView binded to a SimpleCursorAdapter reading data from an SQL table.

I'm try to write a little application and the releated unit tests. I have a Li开发者_开发问答stView binded to a SimpleCursorAdapter reading data from an SQL table.

The Activity#onCreate() method is:

@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);
}

My test inside the unit-test is:

     @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);
     }

My problem is in the third-last row:

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

I sude getChildAt() to get the first TextView child of the ListView. But this method returns null, so the test gets a NullPointerException.

Maybe getChildAt() is not the right method to get the View child from a ListView, so which is the correct one?

I see from the documenation that the method works with GroupView, I didn't use them, do I need to configure a default GroupView and put all the entry inside it? In this way, will getChildAt(0) work? Is this the correct way to setup a ListView?

thank you, bye Andrea

As asked by Vivek, I post here the 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>

As you can see is very very basic. Also le list entry is very simple:

<?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>


I doubt if the list is populated when you call getChildAt() method. So call getChildCount() method and see if the list is populated. And post back the output here.

Edit:

Now I understand the problem. ListView.getCount() method returns the number of items populated in the list. And ListView.getChildCount() Method or ListView.getChildAt() Method will return 0 here because these methods will return a value only when the view is visible to the user. You can use getChildAt() method only after the textviews are generated. i.e If you use the method in OnItemClick method of the listview, or any listview listener implementation, you will get the desired output. What is the need to get the reference to the textviews here in this method anyways?

0

精彩评论

暂无评论...
验证码 换一张
取 消