Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-12114] Android: 'longpress' on tableView with headerView returns incorrect index and might lead to a crash

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2013-06-04T23:12:09.000+0000
Affected Version/sRelease 3.0.0, Release 3.1.0
Fix Version/s2013 Sprint 12 API, 2013 Sprint 12, Release 3.2.0
ComponentsAndroid
Labelscommunity, exalture, module_tableview, qe-testadded
ReporterFederico Casali
AssigneeSunila
Created2012-12-17T15:16:38.000+0000
Updated2014-06-19T12:44:26.000+0000

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)

Attachments

FileDateSize
TestTableView.js2012-12-17T15:16:38.000+00001218

Comments

  1. Peter Colman 2012-12-30

    Does anyone have any idea when or if this problem will be fixed or a patch available?
  2. jithinpv 2013-03-13

    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
  3. Sunila 2013-04-10

    Reduce index by 1 if headerview is present https://github.com/appcelerator/titanium_mobile/pull/4139
  4. Jick Steen 2013-05-25

    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")
       });
        
       
  5. Ingo Muschenetz 2013-05-29

    Going to reopen this one, since it seems the issue is still there.
  6. Sunila 2013-06-01

    Additional fix Removed the redundant index decrement from the longClickHandler also. https://github.com/appcelerator/titanium_mobile/pull/4347
  7. Paras Mishra 2013-10-14

    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

JSON Source