When working with a list view control, you can use specific properties and methods of the Android ListView object that corresponds to this control. This topic explains how to select single and multiple list view items.
Selecting a Single List View Item
To select a single list view item, you can use various actions provided by Android ListView object:
TouchItem
,LongTouchItem
, and similar actions – simulate touch or long touch on a specific list view item.SelectItem
– selects or unselects the specified list view item.
All these actions have the Item parameter that specifies the caption or zero-based index of the desired item. Note that it is possible to use wildcards (* and ?) or regular expressions to specify an item’s caption. For more information, see Addressing ListView Items.
The following example demonstrates how you can use the mentioned actions in scripts:
JavaScript, JScript
function Test()
{
// Select an Android device
Mobile.SetCurrent("MyDevice");
// Obtain an application
var app = Mobile.Device().Process("com.example.myapp");
// Obtain a list view
listViewObj = app.Layout("layoutListView").ListView("lv1");
// Touch some items
listViewObj.TouchItem(0);
listViewObj.LongTouchItem(3);
// Select an item
listViewObj.SelectItem(4);
}
Python
def Test():
# Select an Android device
Mobile.SetCurrent("MyDevice")
# Obtain an application
app = Mobile.Device().Process("com.example.myapp")
# Obtain a list view
listViewObj = app.Layout("layoutListView").ListView("lv1")
# Touch some items
listViewObj.TouchItem(0)
listViewObj.LongTouchItem(3)
# Select an item
listViewObj.SelectItem(4)
VBScript
Sub Test()
' 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.Layout("layoutListView").ListView("lv1")
' Touch some items
listViewObj.TouchItem(0)
listViewObj.LongTouchItem(3)
' Select an item
listViewObj.SelectItem(4)
End Sub
DelphiScript
procedure Test();
var
app, listViewObj: OleVariant;
begin
// Select an Android device
Mobile.SetCurrent('MyDevice');
// Obtain an application
app := Mobile.Device.Process('com.example.myapp');
// Obtain a list view listViewObj := app.Layout('layoutListView').ListView('lv1');
// Touch some items
listViewObj.TouchItem(0);
listViewObj.LongTouchItem(3);
// Select an item
listViewObj.SelectItem(4);
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 list view var listViewObj = app["Layout"]("layoutListView")["ListView"]("lv1");
// Touch some items
listview["TouchItem"](0);
listview["LongTouchItem"](3);
// Select an item
listview["SelectItem"](4);
}
Selecting Custom Items Using Native Methods
Currently, TestComplete does not support custom items of a list view. However, you can obtain it using the Object Spy window. To learn how to do this, see Addressing Objects in Android Open Applications (Legacy).
Custom items are represented as child objects of a list view object. Note, that items that are out of the screen are not available in the Object Browser. To reach the desired items that are out of the screen, you can use the smoothScrollToOffset
native method. This method scrolls the list view to the specified items offset. The following code snippet shows how you can use it:
listViewObj.smoothScrollToOffset(1) // Scroll the list view to one item down
listViewObj.smoothScrollToOffset(-1) // Scroll the list view to one item up
listViewObj.smoothScrollToOffset(listViewObj.ChildCount) // Scroll the list view to the next items, that can be shown on the screen
The collection of the list view's child objects contains only those objects that are visible on screen. When you scroll the list view, the child objects that leave the list area are deleted from the object hierarchy. They are replaced by other child objects that, most likely, have the same recognition attributes. |
To learn if you can scroll to the specified item offset, you can use the canScrollVertically
method.
The following example demonstrates how you can obtain the text of the Button control that is included in a custom item of the list view:
JavaScript, JScript
function test()
{
// Select an Android device
Mobile.SetCurrent("MyDevice");
// Obtain an application
var app = Mobile.Device().Process("com.example.myapp");
// Obtain a list view
var listViewObj = app.RootLayout("").Layout("layoutTop").ListView("lv1");
// Obtain the button control
var btn = listViewObj.Layout("NO_ID").Button("button1");
// Obtain the text of the button
var btnText = btn.ControlText;
// Posts the name to the log
Log.Message(btnText);
}
Python
def Test():
# Select an Android device
Mobile.SetCurrent("MyDevice")
# Obtain an application
app = Mobile.Device().Process("com.example.myapp")
# Obtain a list view
listViewObj = app.RootLayout("").Layout("layoutTop").ListView("lv1")
# Obtain the button control
btn = listViewObj.Layout("NO_ID").Button("button1")
# Obtain the text of the button
btnText = btn.ControlText
# Posts the name to the log
Log.Message(btnText)
VBScript
Sub Main()
' 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("").Layout("layoutTop").ListView("lv1")
' Obtain the button control
Set btn = listViewObj.Layout("NO_ID").Button("button1")
' Obtain the text of the button
btnText = btn.ControlText
' Posts the name to the log
Log.Message(btnText)
End Sub
DelphiScript
procedure Test();
var
app, listViewObj, btn, btnText: OleVariant;
begin
// Select an Android device
Mobile.SetCurrent('MyDevice');
// Obtain an application
app := Mobile.Device.Process('com.example.myapp');
// Obtain a list view
listViewObj := app.RootLayout('').Layout('layoutTop').ListView('lv1');
// Obtain the button control
btn := listViewObj.Layout('NO_ID').Button('button1');
// Obtain the text of the button
btnText := btn.ControlText;
// Posts the name to the log
Log.Message(btnText);
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 list view
var listViewObj = app["RootLayout"]("")["Layout"]("layoutTop")["ListView"]("lv1");
// Obtain the button control
var btn = listViewObj["Layout"]("NO_ID")["Button"]("button1");
// Obtain the text of the button
var btnText = btn["ControlText"];
// Posts the name to the log
Log["Message"](btnText);
}
Selecting Multiple Items
If a list view control supports multiple selection, you can select several items at a time using SelectItem
and MultiSelect
actions.
To select or unselect all the items in the list view, use the SelectItem
action with the Item parameter set to -1
.
listview.SelectItem(-1, true) // Select all the items
listview.SelectItem(-1, false) // Deselect all the items
To select several items in the control, use the MultiSelect
action. You can specify items using an array of their indexes or by their captions separated by the pipeline character (“|”).
The following code snippet demonstrates how to select a set of items:
JavaScript, JScript
var items = [0, 2, 5];
listViewObj.MultiSelect(items);
listViewObj.MultiSelect("Item1|Item4|Item24")
Python
items = [0, 2, 5]
listViewObj.MultiSelect(items)
listViewObj.MultiSelect("Item1|Item4|Item24")
VBScript
items = Array(0, 2, 5)
listViewObj.MultiSelect(items)
listViewObj.MultiSelect("Item1|Item4|Item24")
DelphiScript
listViewObj.MultiSelect(items);
listViewObj.MultiSelect('Item1|Item4|Item24');
C++Script, C#Script
var items = [0, 2, 5];
listViewObj["MultiSelect"](items);
listViewObj["MultiSelect"]("Item1|Item4|Item24")
Simulating Actions From Keyword Tests
This topic explains how to select items of the list view control in scripts. You can use the described actions 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
Checking List View Items' State
Android ListView Support
TouchItem Method (Mobile Controls)
LongTouchItem Action (Android Controls)
SelectItem Action (Android Controls)