Checking List View Items' State

Applies to TestComplete 15.47, last modified on January 20, 2023

In your tests, you may need to check the state of a list view item. For example, you may need to check if the item is selected before performing certain actions.

The Android ListView object provides special properties that you can use to determine the current state of list view items:

  • wSelected - This property has an Item parameter that specifies the desired list view item. This parameter can take the item's caption or its index (see Addressing List View Items). The property returns True if the specified item is selected, and False otherwise.

  • wFocus - Returns the index of the list view item with focus.

Note: Do not confuse the selected item and the focused one. The selected item is checked, while the focused item has focus. Focus navigation allows you to get to an item using directional controllers (arrow keys, a trackball, a directional pad and so on).

The example below iterates through list view items and posts their state. It also posts the index of the focused item.

JavaScript, JScript

function CheckState()
{
  // Select an Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain an application
  var app = Mobile.Device().Process("com.example.myapp");

  // Obtain a listView
  var listViewObj = app.RootLayout("").ListView('lv1');
 
  for (i=0; i < listViewObj.wItemCount; i++)
  {
    // Check the item’s state
    if (listViewObj.wSelected(i))
    {
      Log.Message(listViewObj.wItem(i) + " item is checked");
    }
    else
    {
      Log.Message(listViewObj.wItem(i) + " is not checked");
    }
  }

  // Obtain the index of item with focus
  var focus = listViewObj.wFocus;
  if (focus == -1)
  {
    Log.Message("There are no focused items.");
  }
  else
  {
    Log.Message("The index of the focused item is " + focus);
  }
}

Python

def CheckState():
  # Select an Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain an application
  app = Mobile.Device().Process("com.example.myapp")

  # Obtain a listView
  listViewObj = app.RootLayout("").ListView('lv1')
 
  for i in range(0, listViewObj.wItemCount-1): 
    # Check the items state 
    if (listViewObj.wSelected[i]):
      Log.Message(listViewObj.wItem[i] + " item is checked")
    else:
      Log.Message(listViewObj.wItem[i] + " is not checked")

  # Obtain the index of item with focus
  focus = listViewObj.wFocus
  if (focus == -1):
    Log.Message("There are no focused items.")
  else:
    Log.Message("The index of the focused item is " + focus)

VBScript

Sub CheckState()
  ' Select an Android device
  Mobile.SetCurrent("MyDevice")

  ' Obtain an application
  Set app = Mobile.Device.Process("com.example.myapp")

  ' Obtain a list view
  Set listViewObj = app.RootLayout("").ListView("lv1")

  For i=0 to i < listViewObj.wItemCount -1
    ' Check the item’s state
    If (listViewObj.wSelected(i)) Then
      Log.Message(listViewObj.wItem(i) & " item is checked")
    Else
      Log.Message(listViewObj.wItem(i) & " is not checked")
    End If
  Next
  
  ' Obtain the index of item with focus
  focus = listViewObj.wFocus
  If focus = -1 Then
    Log.Message("There are no focused items.")
  Else
    Log.Message("The index of the focused item is " & focus)
  End If
End Sub

DelphiScript

procedure CheckState();
var
  app, listViewObj, i, focus;
begin
  // Select an Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain an application
  app := Mobile.Device.Process('com.example.myapp');

  // Obtain a listView
  listViewObj := app.RootLayout('').ListView('lv1');
  
  for i:=0 to listViewObj.wItemCount - 1 do
  begin
    
    // Check the item’s state
    if listViewObj.wSelected(i) then 
    begin
      Log.Message(listViewObj.wItem(i) + ' item is checked');
    end
    else Log.Message(listViewObj.wItem(i) + ' is not checked');
  end;
  
  // Obtain the index of item with focus
  focus := listViewObj.wFocus;
  if focus = -1 then
  begin
    Log.Message('There are no focused items.');
  end
  else
    Log.Message('The index of the focused item is ' + focus);
end;

C++Script, C#Script

function CheckState()
  {
  // Select an Android device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain an application
  var app = Mobile["Device"]["Process"]("com.example.myapp");

  // Obtain a listView
  var listViewObj = app["RootLayout"]("")["ListView"]('lv1');
  
  for (i=0; i < listViewObj.wItemCount; i++)
  {
    // Check the item’s state
    if (listViewObj.wSelected(i))
    {
      Log.Message(listViewObj.wItem(i) + " item is checked");
    }
    else
    {
      Log.Message(listViewObj.wItem(i) + " is not checked");
    }
  }
  
  // Obtain the index of item with focus
  var focus = listViewObj.wFocus;
  if (focus == -1)
  {
    Log.Message("There are no focused items");
  }
  else
  {
    Log.Message("The index of the focused item is " + focus);
  }
}

Simulating Actions From Keyword Tests

This topic explains how to check the state of the list view items in scripts. You can use the described properties in keyword tests too. To do this, use the On-Screen Action or the Call Object Method operations.

See Also

Working With Android List View Controls
Addressing List View Items
wSelected Property (Android Controls)
wFocus Property (Android Controls)

Highlight search results