Iterating Through Cells in Android Grid View Controls

Applies to TestComplete 15.68, last modified on October 03, 2024

Iterating through grid view cells means accessing cells in series, one by one. You may need to iterate through grid view cells if you need, for example, to locate a particular cell in the grid, obtain its value or perform some action on it. This topic describes how you can do this.

Iterating Through Grid Cells

To determine the number of grid rows and columns, you can use the wRowCount and wColumnCount properties of the Android GridView object. Once you have determined the number of data rows (columns) in the grid, you can iterate through them in a loop. Since the numeration of them is zero-based, the index of the first element is 0, and the index of the last row (column) is wRowCount-1 (wColumnCount-1. On each loop iteration, you can perform the needed actions with the current cell. For example, you can obtain its value or simulate a user action. To learn how to obtain grid cell values, see Obtaining Cell Values in Android Grid View Controls.

Below is an example that iterates through grid cells and posts their captions to the test log.

JavaScript, JScript

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

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

  // Obtain a grid view
  var gridViewObj = app.RootLayout("").Layout("layoutTop").GridView("gv_simple");
  
  // Iterate through grid view rows
  for (i=0; i<gridViewObj.wRowCount; i++)
  {
    // Iterate through grid view columns
    for (j=0; j<gridViewObj.wColumnCount; j++)
    {
      // Obtain value of the cell
      var CellText = gridViewObj.wValue(i ,j);

      // Print the value to the test log
      Log.Message(CellText);
    }
  }
}

Python

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

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

  # Obtain a grid view 
  gridViewObj = app.RootLayout("").Layout("layoutTop").GridView("gv_simple")
  
  # Iterate through grid view rows
  for i in range(0, gridViewObj.wRowCount-1):
    # Iterate through grid view columns
    for j in range(0, gridViewObj.wColumnCount-1):
      # Obtain value of the cell
      CellText = gridViewObj.wValue[i ,j]

      # Print the value to the test log
      Log.Message(CellText)

VBScript

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

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

  ' Obtain a grid view
  Set gridViewObj = app.RootLayout("").Layout("layoutTop").GridView("gv_simple")
  
  ' Iterate through grid view rows
  For i=0 to gridViewObj.wRowCount-1
    ' Iterate through grid view columns
    For j=0 to gridViewObj.wColumnCount-1
      ' Obtain value of the cell
      CellText = gridViewObj.wValue(i ,j)

      ' Print the value to the test log
      Log.Message(CellText)
    Next
  Next
End Sub

DelphiScript

procedure Test();
var
  app, gridViewObj, i, j, CellText: OleVariant;
begin
  
  // Select an Android device
  Mobile.SetCurrent('MyDevice');

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

  // Obtain a grid view
  gridViewObj := app.RootLayout('').Layout('layoutTop').GridView('gv_simple');
  
  // Iterate through grid view rows
  for i:=0 to gridViewObj.wRowCount-1 do
  begin
    // Iterate through grid view columns
    for j:=0 to gridViewObj.wColumnCount-1 do
    begin
      // Obtain value of the cell
      CellText := gridViewObj.wValue(i ,j);

      // Print the value to the test log
      Log.Message(CellText);
    end;
  end;
end;

C++Script, C#Script

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

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

  // Obtain a grid view
  var gridViewObj = app["RootLayout"]("")["Layout"]("layoutTop")["GridView"]("gv_simple");
  
  // Iterate through grid view rows
  for (i=0; i<gridViewObj["wRowCount"]; i++)
  {
    // Iterate through grid view columns
    for (j=0; j<gridViewObj["wColumnCount"]; j++)
    {
      // Obtain value of the cell
      var CellText = gridViewObj["wValue"](i ,j);

      // Print the value to the test log
      Log["Message"](CellText);
    }
  }
}

Simulating Actions From Keyword Tests

This topic explains how to iterate through cells of the grid view control 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 Grid View Controls
Obtaining Cell Values in Android Grid View Controls
Searching for Rows in Android Grid View Controls
wRowCount Property (Android Controls)
wColumnCount Property (Android Controls)

Highlight search results