简体   繁体   中英

Store value temporarily

I am working on showing information on textview. I have two tables which each containing list of informations. If I click "table A" button it shows the 1st information and if I click it again it shows the 2nd information. Then if I click the "table b" button then the 1st information of b is shown and it increases as the number of clicks increases. But if I click "table a" button again, then it must show the second information, not the third one. I am not getting any idea how to do that one. Please help me. I am new. My piece of code for that is as follows

case "table a":
    if (j>4) {
        j=1;
    }
    text.setText(String.valueOf(j));
    j++;
    Toast.makeText(getApplicationContext(), 
            "A table information", Toast.LENGTH_SHORT).show();
    break;

case "table b":
    if (i>6) {
        i=1;
    }
    text.setText(String.valueOf(j));
    i++;
    Toast.makeText(getApplicationContext(), 
            "B table information", Toast.LENGTH_SHORT).show();
    break;

The problem I am facing is, if I click the "table a". it shows the 1st information. And then if I click "table b" then it shows the 1st information of table b. Then if I click the "table a" it is showing 2nd. I want it to show 1st information, if I come back from "table b" to "table a". Please give some suggestion.

Java is by default zero based, thus if you reset an array, you should set the value to 0, not 1.

if(j>4){
   j=0;
}

As I understand it, you want to show the same thing that was previously shown on table A if you switch back to it. The best way to do that is to keep track of the active state, and only change the value if the active state is the current one. The best way to do this is with some sort of an enum. This code might need a few tweeks, such as using TABLE_SELECTED.A instead of just TABLE_SELECTED, but it should work with such tweaks done.

private enum TABLE_SELECTED{A,B};
TABLE_SELECTED activeTAble=A;
case "table a":  if(aIndex>4){
                aIndex=0;
            }
            text.setText(String.valueOf(aIndex));
            if(activeTable==A)
                aIndex++;
            activeTable=A
                Toast.makeText(getApplicationContext(), "on clicked", Toast.LENGTH_SHORT).show();

case "table b":   if(bIndex>6){
                bIndex=1;
            }
            text.setText(String.valueOf(bIndex));
            if(activeTable==B)
                bIndex++;
            activeTable=B
                Toast.makeText(getApplicationContext(), "on clicked", Toast.LENGTH_SHORT).show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM