App crash after swipe to empty Arraylist [ ] Gesture Top & Bottom

时间:2019-03-06 11:39:51

标签: android arrays crash swipe

I'm working on a little app for my training of Android developer.

Actualy i'm stuck when i swipe from bottom to top and top to bottom.

I would like to infinite swipe the [ images ] 0 to 4.

If i exeed the limit of the Array -1 or +5 the app crash.

This short code works well but i miss [0] from bottom to top

and i miss [4] from top to bottom.

Please can you help me, i'm stuck on it 2 days.

Thanks you very much.

// A SIMPLE COUNTER

  int counter = 0;

// ARRAY OF SMILEYS LIST

    int[] arraySmileys = new int[]{
            R.drawable.smiley_super_happy,
            R.drawable.smiley_happy,
            R.drawable.smiley_normal,
            R.drawable.smiley_disappointed,
            R.drawable.smiley_sad,
    };

//ACTION ON SWIPE BOTTOM TO TOP & TOP TO BOTTOM

    String message = "";


    switch (direction) {


        case BOTTOM_TO_TOP:
            if (counter < 4) {
                counter++;
                imagePic.setImageResource(arraySmileys[counter]);
                message = String.valueOf(counter);

            } if (counter == 4) {
               counter = 0;
               imagePic.setImageResource(arraySmileys [counter]);
               message = String.valueOf(counter);
            }


            break;


        case TOP_TO_BOTTOM:
            if (counter > 0) {
                counter --;
                imagePic.setImageResource(arraySmileys[counter]);
                message = String.valueOf(counter);

            } if (counter == 0) {
                counter = 4;
                imagePic.setImageResource(arraySmileys[counter]);
                message = String.valueOf(counter);
            }


            break;


    }


    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();


}




}

RESOLVED

 int counter = 0;

// ARRAY OF SMILEYS LIST

int[] arraySmileys = new int[]{
        R.drawable.smiley_super_happy,
        R.drawable.smiley_happy,
        R.drawable.smiley_normal,
        R.drawable.smiley_disappointed,
        R.drawable.smiley_sad,
};



switch (direction) {


        case BOTTOM_TO_TOP:
            if (counter > 0) {
                counter--;
                imagePic.setImageResource(arraySmileys[counter]);
                message = String.valueOf(counter);

            } else {
                counter = 4;
                imagePic.setImageResource(arraySmileys[counter]);
                message = String.valueOf(counter);
            }
            break;


        case TOP_TO_BOTTOM:
            if (counter < 4) {
                counter++;
                imagePic.setImageResource(arraySmileys[counter]);
                message = String.valueOf(counter);

            } else {
                counter = 0;
                imagePic.setImageResource(arraySmileys[counter]);
                message = String.valueOf(counter);
            }
            break;
    }
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}

}

0 个答案:

没有答案