This is a migrated thread and some comments may be shown as answers.

iOS: listview.getItemAtIndex is not a function

1 Answer 66 Views
ListView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Marc
Top achievements
Rank 1
Marc asked on 02 Jun 2017, 10:38 AM

Hello,

I notice a weird issue with the ListViewEventData.

Here is my ListView in XML:

<lv:RadListView
    items="{{ favorites }}"
    itemTap="onItemTap"
    itemLoading="onItemLoading">
    <lv:RadListView.listViewLayout>
        <lv:ListViewLinearLayout scrollDirection="Vertical" />
    </lv:RadListView.listViewLayout>
    <lv:RadListView.itemTemplate>
       <StackLayout>
           <GridLayout rows="auto" columns="*,10,auto">
             <!-- ... -->
           </GridLayout>
           <StackLayout className="separator" />
        </StackLayout>
     </lv:RadListView.itemTemplate>
</lv:RadListView>

 

Here is my tap event:

exports.onItemTap = function(args) {
    var listview = args.object.parent;
    var bindingContext = args.object.bindingContext;
    var currentItem = listview.getItemAtIndex(args.itemIndex);
};

 

The problem:

I try to store a reference to the RadListView instance, that has triggered the tap-event. While the above code is working as expected, the app crashes on Android
 with the error message listview.getItemAtIndex is not a function.

I noticed, that args.object.parent.parent would point to the RadListView instance on Android.

We have a TabView with about 4-5 Tabs and each of them contains one RadListView with different data, so I don't want to apply an ID to each TabView to access it. I thought the event-arguments contain the information, which RadListView has triggered the event.

Now I do not know if this is an already known bug or if this refers to my own code in any way.

And when I raise the itemSelected event, then args.object points to the corresponding RadListView instance for both, Android and iOS... which is a bit confusing.

 

We are working with:

  • NativeScript 2.5 (tns-core-modules 2.5.2)
  • Telerik UI for NativeScript 1.6.1

 

Best regards,

Marc

1 Answer, 1 is accepted

Sort by
0
Nikolay Tsonev
Telerik team
answered on 05 Jun 2017, 07:41 AM
Hi,
Thank you for contacting us.

I reviewed your case and indeed the parent property will return a reference to different objects while using the property on Android and iOS. This issue a valid problem also in the new version 2.0.1 and it has been already reported in this issue in repository. 

With the upcoming release 3.0.0, args.object property will refer to the RadListView and there will be no need top access the component while using parent 
property.
Also, this problem has been already fixed and you could test it on your side while using our @next tag.
tns plugin remove nativescript-telerik-ui-pro
 
tns plugin add nativescript-telerik-ui-pro

If this is not applicable for you, meanwhile, you could use the platform module and check about the platform type. In this case, you will able to get the Listview by using args.object.parent.parent and for iOS - args.object.parent. For example:


var platform_1 = require("platform");
function onPageLoaded(args) {
    var page = args.object;
    page.bindingContext = new viewModel.ViewModel();
}
exports.onPageLoaded = onPageLoaded;
 
function onItemTap(args) {
 
    var listview;
 
    if (platform_1.isAndroid) {
        listview = args.object.parent.parent;
    }
    else {
        listview = args.object.parent;
    }
}
exports.onItemTap = onItemTap;

Hope this will help.
Regards,
nikolay.tsonev
Progress Telerik
Did you know that you can open private support tickets which are reviewed and answered within 24h by the same team who built the components? This is available in our UI for NativeScript Pro + Support offering.
Tags
ListView
Asked by
Marc
Top achievements
Rank 1
Answers by
Nikolay Tsonev
Telerik team
Share this question
or