Titanium Mobile
  1. Titanium Mobile
  2. TIMOB-12114

Android: 'longpress' on tableView with headerView returns incorrect index and might lead to a crash

    Details

      Description

      Problem description

      If the tableview has a header, e.index returns the incorrect index for the 'longpress' event.

      If the first row is long-pressed, then e.index == 1 instead of 0. 'click' returns the correct index. If the last row is long-pressed, the SDK crashes with an IndexOutOfBoundsException. If the TableView is created without a headerView, the index values are correct.

      Only reproducible on TiSDK 3.0 as with a previous version e.index is undefined.
      Works fine on iOS.

      Steps to reproduce

      var _self = Ti.UI.createWindow({
      	backgroundColor : '#fff',
      	height : Ti.UI.FILL,
      	width : Ti.UI.FILL
      });
      var title = "TableView Bug";
      var win = Ti.UI.createView({});
      _self.add(win);
      
      var table = Ti.UI.createTableView({
      	headerTitle : title
      });
      var headerView = Ti.UI.createView({
      	backgroundColor : '#444'
      });
      var titleLabel = Ti.UI.createLabel({
      	width : _self.width,
      	textAlign : 'center',
      	color : '#eee'
      });
      headerView.add(titleLabel);
      titleLabel.text = "Title text";
      table.setHeaderView(headerView);
      
      var data = [];
      data.push(Ti.UI.createTableViewRow({
      	title : "Row 0"
      }));
      data.push(Ti.UI.createTableViewRow({
      	title : "Row 1"
      }));
      data.push(Ti.UI.createTableViewRow({
      	title : "Row 2"
      }));
      data.push(Ti.UI.createTableViewRow({
      	title : "Row 3"
      }));
      table.setData(data);
      
      win.add(table);
      
      var label = Ti.UI.createLabel({
      	height : '40dp',
      	bottom : 0,
      	text : "Label"
      });
      win.add(label);
      
      table.addEventListener('click', function(e) {
      	Ti.API.info("#### table - row " + e.index + " clicked")
      });
      table.addEventListener('longpress', function(e) {
      	Ti.API.info("#### table - row " + e.index + " longpressed")
      });
      
      _self.open();
      
      

      Pressing and long-pressing on the rows shows the index being returned.
      Long pressing the last row causes the crash:

      ==
      E/AndroidRuntime( 8867): java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5
      E/AndroidRuntime( 8867): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
      E/AndroidRuntime( 8867): at java.util.ArrayList.get(ArrayList.java:304)
      E/AndroidRuntime( 8867): at ti.modules.titanium.ui.widget.tableview.TiTableView.getItemAtPosition(TiTableView.java:411)
      E/AndroidRuntime( 8867): at ti.modules.titanium.ui.TableViewProxy.fireEvent(TableViewProxy.java:149)
      E/AndroidRuntime( 8867): at org.appcelerator.titanium.view.TiUIView$3.onLongPress(TiUIView.java:1156)
      E/AndroidRuntime( 8867): at android.view.GestureDetector.dispatchLongPress(GestureDetector.java:1451)
      E/AndroidRuntime( 8867): at android.view.GestureDetector.access$200(GestureDetector.java:50)
      E/AndroidRuntime( 8867): at android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:319)
      E/AndroidRuntime( 8867): at android.os.Handler.dispatchMessage(Handler.java:99)
      E/AndroidRuntime( 8867): at android.os.Looper.loop(Looper.java:137)
      E/AndroidRuntime( 8867): at android.app.ActivityThread.main(ActivityThread.java:4898)
      E/AndroidRuntime( 8867): at java.lang.reflect.Method.invokeNative(Native Method)
      E/AndroidRuntime( 8867): at java.lang.reflect.Method.invoke(Method.java:511)
      E/AndroidRuntime( 8867): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
      E/AndroidRuntime( 8867): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
      E/AndroidRuntime( 8867): at dalvik.system.NativeStart.main(Native Method)

        Issue Links

          Activity

          Hide
          Peter Colman added a comment -

          Does anyone have any idea when or if this problem will be fixed or a patch available?

          Show
          Peter Colman added a comment - Does anyone have any idea when or if this problem will be fixed or a patch available?
          Hide
          jithinpv added a comment -

          Issue reproduces with

          Titanium Studio, build: 3.0.1.201212181159
          Titanium SDK version: 3.1.0 (03/11/13 15:43 0c88429)
          Titanium SDK version: 3.0.2 (02/07/13 16:46 a4def81)
          Device: Samsung galaxy s duos Android version: 4.0.4
          Android Emulator: Android SDK version: 2.2

          Show
          jithinpv added a comment - Issue reproduces with Titanium Studio, build: 3.0.1.201212181159 Titanium SDK version: 3.1.0 (03/11/13 15:43 0c88429) Titanium SDK version: 3.0.2 (02/07/13 16:46 a4def81) Device: Samsung galaxy s duos Android version: 4.0.4 Android Emulator: Android SDK version: 2.2
          Hide
          Sunila added a comment -

          Reduce index by 1 if headerview is present
          https://github.com/appcelerator/titanium_mobile/pull/4139

          Show
          Sunila added a comment - Reduce index by 1 if headerview is present https://github.com/appcelerator/titanium_mobile/pull/4139
          Hide
          Jick Steen added a comment -

          The app is still crashing on latest 3.2 build if you remove the headerTitle. Tried it on a Samsung Galaxy S2 with Android 4.1.2.

          Code to reproduce:

          var _self = Ti.UI.createWindow({
              backgroundColor : '#fff',
              height : Ti.UI.FILL,
              width : Ti.UI.FILL
          });
          _self.open();
          
          var table = Ti.UI.createTableView();
          
          var headerView = Ti.UI.createView({
              backgroundColor : '#444', visible: false, height: 0
          });
          var titleLabel = Ti.UI.createLabel({
              width : _self.width,
              textAlign : 'center',
              color : '#eee',
              text: "Title text"
          });
          headerView.add(titleLabel);
          table.setHeaderView(headerView);
          
          var row = Ti.UI.createTableViewRow({title: 'Press me long'});
          table.setData([row]);
           
          _self.add(table);
           
          table.addEventListener('click', function(e) {
              Ti.API.info("#### table - row " + e.index + " clicked")
          });
          table.addEventListener('longpress', function(e) {
              Ti.API.info("#### table - row " + e.index + " longpressed")
          });
           
          
          Show
          Jick Steen added a comment - The app is still crashing on latest 3.2 build if you remove the headerTitle. Tried it on a Samsung Galaxy S2 with Android 4.1.2. Code to reproduce: var _self = Ti.UI.createWindow({ backgroundColor : '#fff', height : Ti.UI.FILL, width : Ti.UI.FILL }); _self.open(); var table = Ti.UI.createTableView(); var headerView = Ti.UI.createView({ backgroundColor : '#444', visible: false, height: 0 }); var titleLabel = Ti.UI.createLabel({ width : _self.width, textAlign : 'center', color : '#eee', text: "Title text" }); headerView.add(titleLabel); table.setHeaderView(headerView); var row = Ti.UI.createTableViewRow({title: 'Press me long'}); table.setData([row]); _self.add(table); table.addEventListener('click', function(e) { Ti.API.info("#### table - row " + e.index + " clicked") }); table.addEventListener('longpress', function(e) { Ti.API.info("#### table - row " + e.index + " longpressed") });
          Hide
          Ingo Muschenetz added a comment -

          Going to reopen this one, since it seems the issue is still there.

          Show
          Ingo Muschenetz added a comment - Going to reopen this one, since it seems the issue is still there.
          Hide
          Sunila added a comment -

          Additional fix
          Removed the redundant index decrement from the longClickHandler also.

          https://github.com/appcelerator/titanium_mobile/pull/4347

          Show
          Sunila added a comment - Additional fix Removed the redundant index decrement from the longClickHandler also. https://github.com/appcelerator/titanium_mobile/pull/4347
          Hide
          Paras Mishra added a comment -

          Longpress event is returning the correct index.

          Verified fix on:
          Device : Google Nexus 7, Android Version: 4.3
          SDK: 3.2.0.v20131013140318
          CLI version : 3.2.0
          OS : MAC OSX 10.8.4
          Alloy : 1.2.2
          Appcelerator Studio, build: 3.2.0.201310112258
          XCode : 5

          Show
          Paras Mishra added a comment - Longpress event is returning the correct index. Verified fix on: Device : Google Nexus 7, Android Version: 4.3 SDK: 3.2.0.v20131013140318 CLI version : 3.2.0 OS : MAC OSX 10.8.4 Alloy : 1.2.2 Appcelerator Studio, build: 3.2.0.201310112258 XCode : 5

            People

            • Assignee:
              Sunila
              Reporter:
              Federico Casali
              Reviewer:
              Allen Yeung
            • Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: