0

I don't get the correct index. Can anyone tell me why? I expect 0 as index, not -1.

int[] id = {R.id.buKeyPadNum1, ...} **(EDIT)**
int index = Arrays.asList(id).indexOf(v.getId());

Log.i("onClick", "v.getId "+ String.valueOf(v.getId())+" ButtonId "+String.valueOf(id[0])
+" Index "+String.valueOf(index));

LogCat: 09-25 08:11:32.039: I/onClick(1680): v.getId 2131296337 ButtonId 2131296337 Index -1

2 Answers2

1

Consider such code:

int ix = Arrays.asList(new int[]{1,2}).indexOf(1); 
// result: -1

int ix = Arrays.asList(new Integer[]{1,2}).indexOf(1); 
// result: 0  (found)

Probably your array contains elements with different type from indexOf argument.

przemek hertel
  • 3,924
  • 1
  • 19
  • 27
1

Arrays.asList(id) returns a List<long[]>. indexOf() only works on Long[] objects. See this answer.

Try using the Long[] type for id.

Community
  • 1
  • 1
Nachi
  • 4,218
  • 2
  • 37
  • 58