Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-16079] Android: Incorrect e.itemIndex for ListView and SearchBar

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2014-08-20T00:58:36.000+0000
Affected Version/sRelease 3.2.0
Fix Version/sRelease 3.4.0
ComponentsAndroid
Labelsmodule_listview, qe-manualtest
ReporterTim Fisher
AssigneeSunila
Created2013-12-18T08:58:25.000+0000
Updated2014-08-22T16:04:37.000+0000

Description

Steps to Reproduce

Create a SearchBar and ListView in Alloy, populate with searchable data. Enter search query in SearchBar and click top item. iOS returns the correct index of the clicked item, Android will return an index that is relative to the filtered list. i.e. the top item will always have an itemIndex of 0 on Android.

Actual Result

0

Expected Result

other than 0

Attachments

FileDateSize
.log2013-12-18T08:58:30.000+00002893559
diagnostic3273972563179129461.log2013-12-18T08:58:34.000+000019033

Comments

  1. Ritu Agrawal 2013-12-22

    Thank you, Tim, for reporting this issue. I would appreciate if you can provide a simple test case to reproduce the issue as that would help us in troubleshoot the issue.
  2. Ray Belisle 2014-01-02

    Here is an example that returns the correct item on iOS and shows the issue on Android.
       var win = Ti.UI.createWindow({backgroundColor: 'white', fullscreen: true});
       
       var search = Titanium.UI.createSearchBar({
           barColor:'#000', 
           showCancel:true,
           height:43,
           top:0,
       });
       search.addEventListener('cancel', function(){
           search.blur();
       });
       // for textSearch, use the change event to update the search value
       // search.addEventListener('change', function(e){
       //     listView.searchText = e.value;
       // });
       
       var listView = Ti.UI.createListView({searchView: search, caseInsensitiveSearch: true});
       // for textSearch, add the search bar or text field as a header view
       // var listView = Ti.UI.createListView({headerView: search, caseInsensitiveSearch: true});
       
       var listSection = Ti.UI.createListSection();
       var fruits = ['Papaya', 'Peach', 'Pear', 'Persimmon', 'Pineapple', 'Pluot', 'Pomegranate'];
       var data = [];
       for (var i = 0; i < fruits.length; i++) {
           data.push({
               properties: {title: fruits[i], searchableText: fruits[i]}
           });
       }
       listSection.setItems(data);
       
       listView.sections = [listSection];
       
       listView.addEventListener('itemclick', function(e){
           // Only respond to clicks on the label (rowtitle) or image (pic)
       var item = e.section.getItemAt(e.itemIndex);
       alert(item);
       });
       win.add(listView);
       win.open();
       
  3. Tim Fisher 2014-01-22

    Any idea when this will be fixed? I have an app going live at the end of Feb and this needs to be in.
  4. Nguyen Bao Duy 2014-03-19

    Hi guys, I have an app need to release at the end of this March. Can you guys fix it earlier? Or can explain me the reason of this issue and I can play around with it. Thanks so much
  5. Alessio Iacarelli 2014-03-19

    Hi All, Same issue here, is there any workaround available? Thank you very much!
  6. Nguyen Bao Duy 2014-03-20

    I did some trick and it working for me, I tested and found" e.sectionIndex is right. e.itemIndex(wrong) is index of item in results list not original list. So we can use _.filter() to get results manually from original list and SearchText(_results) and item we need will be _results[e.itemIndex]. Code:
       var section = $.container.sections[e.sectionIndex];
       
       if (OS_ANDROID) {
          var key = $.container.getSearchText();
          
          // get results manually
          var _results = _.filter(section.items, function (_item) {
             return _item.properties.searchableText.indexOf(key) !== -1;
          });
       
          // item we need
          item = _results[e.itemIndex];
       }
       
  7. Ingo Muschenetz 2014-04-21

    Deferring to 3.4.0 to properly fix parity issue. Please let us know if the workaround does not work for you.
  8. Mark Henderson 2014-05-21

    Can I please champion this parity bug to be fixed as the work around is a lot of extra code. For anyone not searching but filtering something like this will help:
       
       var section = listView.sections[e.sectionIndex];
       			var key = listView.getSearchText();
       		  	  	
       		   // get results manually - append itemIndex to e
       		   var item;
       		   if (key && key.length > 0) {
       			   for (var i=0; i < section.items.length; i++) {
       			   		if (section.items[i].properties.searchableText.toLowerCase().indexOf(key) !== -1) {
       						if (section.items[i].properties.itemId == e.itemId) {
       				   			item = section.items[i];
       				   			e.itemIndex = i;
       				   			break;
       				   		}	
       			   		}
       				};
       			} else {
       				for (var i=0; i < section.items.length; i++) {
       					if (section.items[i].properties.itemId == e.itemId) {
       						item = section.items[i];
       				   		e.itemIndex = i;
       					}
       				};
       				
       			}
       			
       			if (item && e.itemIndex) {
       				if (item.properties.accessoryType == Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK) {
       					item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_NONE;
       				} else {
       					item.properties.accessoryType = Ti.UI.LIST_ACCESSORY_TYPE_CHECKMARK;
       				}
       			   
       			   e.section.updateItemAt(e.itemIndex,item);
       			}
       
  9. Sunila 2014-06-03

    Modfied the code to store real index instead of the filtered index in the event data https://github.com/appcelerator/titanium_mobile/pull/5755
  10. Lokesh Choudhary 2014-08-20

    Verified the fix. Android returns the right e.itemIndex & not just of the first item. Closing. Environment: Appc Studio : 3.4.0.201408180158 Ti SDK : 3.4.0.v20140815142514 Mac OSX : 10.8.5 Alloy : 1.5.0-dev CLI - 3.4.0-dev Code Processor: 1.1.1 Nexus 5 - android 4.4.4
  11. Neville Dastur 2014-08-22

    Due for release in 3.4.0. REALLY!!!? This is quite a critical bug and I would ask for it to be part of a maintainance release please
  12. Ingo Muschenetz 2014-08-22

  13. Neville Dastur 2014-08-22

    Oh, okay. I saw the PR was 3 months old and having just released 3.3.0.GA I thought 3.4.0 was going to be a while off.

JSON Source