how to check previous value in android of viewflipper

前端 未结 2 1324
借酒劲吻你
借酒劲吻你 2020-12-20 18:54

I Have eight View flipper which have 4 Question and 4 Answers. Which is display those Question and Answers, whenever that particular viewflipper is

相关标签:
2条回答
  • 2020-12-20 19:47

    The problem is that your code is missing the state of the PREVIOUS selected ViewFlipper. I can only provide partial code as i don't know the rest of your code.

    in the class which has your code snippet on here, the "public void onClick(View v) {" method, you want to have something like this:

    public class Puzzle extends Activity{ //or whatever your class is called
    
        private int previousFlipperID = -1; //this stores the previous state of the flipper that was selected
    
        @Override
        public void onClick(View v) {
            //.... your code, but modified
                // changes
            if(previousFlipperID == -1){
                //this is the VERY FIRST CLICK, using -1 to denotate that nothing was previously selected
                previousFlipperID = v.getId();
                return;  
            }
                // end changes
    
            switch (v.getId()) {
            case R.id.vf1:
            try {
                if (click) {
    
                // changes
                    switch(previousFlipperID){
                    case 0: 
                        //do your logic here that you stated. note that at this point in the code
                        // you are at the point where your PREVIOUS flipper was the first flipper
                        // and your CURRENT flipper is also the first one, since v.getId() is R.id.vf1
                        break;
                    case 1:  //some logic, but the CURRENT flipper is the first flipper, while PREVIOUS flipper was the second flipper
                        ...
                    case 7: //the previous flipper selected was the last flipper
                        break;
                    }
                // end changes
    
                    Message msg = new Message();
                    msg.what = 1;
                    delayHandler.sendMessageDelayed(msg, DELAYTIME);
    
                    vFilpper1.showNext();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            counter++;
            break;
    
            // changes
            previousFliperID = v.getId(); //update the previous flipper
            // end changes
    
        }
    }
    

    in my code, look for "//changes" especially. you'll see that i used an integer to store the previous flipper. and then the way i check if it's the FIRST time, is to check if the previous flipper was -1 or not. then at the end, make sure you set the current flipper id as the previous flipper id to update the "previous" flipper id for the next time.

    also, note that i had a nested switch statement in there, because you need a bunch of extra checks to do your logic for what happens, depending on what the current and previous flippers are.

    the answer is to the best of my abilities on information i've been told (and also to the best of my understanding), so i hope this helps.

    P.S. i feel guilty say this, but if my answer was correct, please please please check my answer, because i actually need some bounty points to ask bounty questions but i don't think i have enough points. thanks! and happy holidays

    0 讨论(0)
  • 2020-12-20 19:49

    Okay you have no code that shows how you switch between your views but here it goes: When you switch in code in your on click or whatever you set a membervariable to last = currentselected and currentselected = the id of the selected viewflipper. Really dead simple. Oh and first time you make a check to see if its null. If you want me to be more precise supply code that shows how you switch between your views.

    0 讨论(0)
提交回复
热议问题