Search code examples
javaandroidfor-looparraylisttext-to-speech

I want to read all the list items by swiping touch event


I have a arraylist as messagee and i am using listview so that when I swipe up it will speak one by one item using tts engine. but problems is that it only speaks 0 th index item. but I want like when user swipe first it reads 0 th index item then again swipe ups it reads 1st index item so on.. How to achieve this? Here is my code:-

    public boolean onTouchEvent(MotionEvent touchEvent) {
        switch (touchEvent.getAction()) {

            case MotionEvent.ACTION_DOWN:
                x1 = touchEvent.getX();
                y1 = touchEvent.getY();

                break;
            case MotionEvent.ACTION_UP:
                x2 = touchEvent.getX();
                y2= touchEvent.getY();



                if(y1>y2){
                    int i;
                    for( i =0;i<messagee.size();i++){
                        textTospeech.speak(messagee.get(i),TextToSpeech.QUEUE_FLUSH,null);
                        i=i+1;
                    }



                }


                break;


        }

        return false;
    }


Solution

  • int swipeCount =0;                                            
    public boolean onTouchEvent(MotionEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x1 = touchEvent.getX();
                y1 = touchEvent.getY();
                break;
            case MotionEvent.ACTION_UP:
                x2 = touchEvent.getX();
                y2 = touchEvent.getY();
                if (y1 > y2) {
                    int i;
                    if (swipeCount <= messagee.size()) {
                        textTospeech.speak(messagee.get(swipeCount), TextToSpeech.QUEUE_FLUSH, null);
                        swipeCount++;
                    }
                }
                break;
        }
        return false;
    }