Forum Discussion

kriz6912's avatar
kriz6912
New Contributor
9 years ago

Android: enumerating all items in a ListView (even the off-screen ones).

On Android I can access a ListView and select any of its items (even the none visible ones) without any problem:

var RefListView = "Aliases.Device.Process_qv.RootLayout.View_drawer_layout.ListView('channel_lists')";  
var ObjListView = eval(RefListView);
var ItemCount = ObjListView.wItemCount;
ObjListView.SelectItem(12); // Works OK; even on invisible Items [0 - wItemCount-1]

 

However, I want an inventory of all the names of the list items. Each item in the listview has 3 subitems. The subitem I'm interested in looks like below (I used the 5th ListView Item as example here):

Aliases.Device.Process_qv.RootLayout.View_drawer_layout.ListView('channel_lists').Layout('NO_ID', 4).TextView('text1')

 

So I use following code to read the 'text1' field from all items in the ListView:

for (var ItemIndex=1; ItemIndex <= ItemCount; ItemIndex++)
{ 
var RefItem = "Aliases.Device.Process_qv.RootLayout.View_drawer_layout.ListView('channel_lists').Layout('NO_ID', PLACEHOLDER_INDEX).TextView('text1')";
var ObjItem = eval(RefItem.replace("PLACEHOLDER_INDEX", ItemIndex)); 

Log.Message("ObjItem" + ItemIndex + ":Text=" + ObjItem.mText);
}

 

Unfortunately, this only works for the items that are currently on the screen, but not for the ones that are currently outside the view. What is the recommended method for making an inventory of ALL listItems, including the ones currently not on screen? I read about using 'listviewObj.smoothScrollToOffset' to reach items that are out of the screen (viewarticle/74368/). But when I try this on my listviewObj I get "object does not support this action".

 

Thanks for your help,

2 Replies

  • kriz6912's avatar
    kriz6912
    New Contributor

    As described in viewarticle/74368/ listViewObj.smoothScrollToOffset(listViewObj.ChildCount) seems indeed the ideal solution which will me allow to bring the not visible listitems into view. But as mentioned above, for some reason this method is not available for my ListView Object.

     

    When viewing the methods for the Aliases.Device.Process_qv.RootLayout.View_drawer_layout.ListView("channel_lists") I can only see following methods:
    - smoothScrollBy, ..._2, ...Offset
    - smoothScrollToPosition, smoothScrollToPosition_2, ..._3, ...FromTop, ...Position_2

     

    But none of them apparently allow me to scroll the view based on a listItem index.

  • MarekFric's avatar
    MarekFric
    Occasional Contributor

    Hi Kriz. Everytime when I scroll down I'm checking if desired object is visibleOnScreen(). 

     

    I use following code snippet for scrolling down:

     

    while (!objFound)

    {

       obj = searchForDesiredObj(listView)

       if (obj.Exists) {

         objFound = true

         return true

       }

     

       if (listView.canScrollVertically(1)) {
          listView.smoothScrollByOffset(1)
          listView.Refresh()
       } else {
          Log.Error("Cannot scroll down anymore.")
          return false
      }

    }