nabuko
nabuko

Reputation: 39

How to check if a button is in a array of buttons?

I have two arrays of buttons. How to check if some clicked button is in an array? I tried this, but it didn't work:

private static final int[] idArrayA = {R.id.bA1, R.id.bA2, R.id.bA3, R.id.bA4, R.id.bA5, R.id.bA6, R.id.bA7, R.id.bA8};
private static final int[] idArrayB = {R.id.bB1, R.id.bB2, R.id.bB3, R.id.bB4, R.id.bB5, R.id.bB6, R.id.bB7, R.id.bB8};

final OnClickListener clickListenerA = new OnClickListener(){


        public void onClick(View v) {
               if (Arrays.asList(idArrayA).contains(v.getId())) {

                   Button button = (Button) v;
                   button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));  

               }
        }
       };

Upvotes: 0

Views: 123

Answers (1)

Paweł Wyrwiński
Paweł Wyrwiński

Reputation: 1443

Arrays.asList(idArrayA) will produce List<int[]> instead of List<Integer> hence bad results.
Construct List from your array by simply iterating through it and adding its elements to new List :

List<Integer> arrayAsList = new ArrayList<Integer>(idArrayA.length); // List with initial capacity
for (int i : idArrayA) {
  arrayAsList.add(i);
}

or declare idArrayA as private static final Integer[].

Upvotes: 1

Related Questions