Android: get i-th TextView inside ListView

I am trying to write a small application and released unit tests. I have a ListView bound to SimpleCursorAdapter data reading data from an SQL table.

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

My test inside the unit test:

     @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 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 or last line:

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

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

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

, GroupView, , ? , getChildAt (0) ? ListView?

, Andrea

, 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>
+3
1

, , getChildAt(). getChildCount() , . .

Edit:

. ListView.getCount() , . ListView.getChildCount() ListView.getChildAt() 0 , , . getChildAt() . OnItemClick listview listview listener, . ?

+6

Source: https://habr.com/ru/post/1782162/


All Articles