0

Hello i have a problem in this code i make a true or false game on andorid the problem is this practically the game onward the 2 round because the variable(int round) is not increase the value of variable stay always 2 then Never goes to the third question

public class Livello extends Activity {
int livello;
int punteggio;
Verifica verifico = new Verifica();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_livello);
    Intent intent =getIntent();
    String pkg=getPackageName();
    livello =intent.getIntExtra(pkg+".myInt",-1);
    if(livello==1) {
        Start1();
    }
}


  public void Start1(){
   verifico.question = (TextView) findViewById(R.id.domande);
   verifico.vero =(Button)findViewById(R.id.vero);
   verifico.falso =(Button)findViewById(R.id.falso);
   punteggio= verifico.punteggio;
   verifico.Controllo1();
 }

}

verifica.class

public class Verifica extends Activity{
  Button vero;
  Button falso;
  TextView question;
  int punteggio;
  int round;
  InizioRound inizioround = new InizioRound();

public void Controllo1(){
inizioround.question=question;
round=1;
round = inizioround.livello1(1);
vero.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        punteggio++;
        round = inizioround.livello1(2);
    }
});
falso.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        round = inizioround.livello1(2);
    }
});
if(round == 2){

    vero.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            punteggio++;
            round = inizioround.livello1(3);
            inizioround.livello1(3);
            Log.e("vaa", "round=" + round);
        }
    });
}  else if(round == 3){
    round = inizioround.livello1(3);
    falso.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            punteggio++;round = inizioround.livello1(4);
        }
    });
} else if(round == 4){
    falso.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            punteggio++;round = inizioround.livello1(5);
        }
    });
} else if(round == 5){
    round = inizioround.livello1(5);
    falso.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            punteggio++;round = inizioround.livello1(5);
        }
    });
   }
  }
}

inizioround.class

public class InizioRound extends Activity{
TextView question;
public int livello1(int round){
    if(round == 1){
        question.setText(R.string.dl1);
        round=1;
    } else if(round == 2){
        question.setText(R.string.dl2);
        round=2;
    } else if(round == 3){
        question.setText(R.string.dl3);
        round=3;
    } else if(round == 4){
        question.setText(R.string.dl4);
        round=4;
    } else if(round == 5){
        question.setText(R.string.dl5);
        round=5;
    }
    return round;
 }
}
  • 1
    "the value of variable" - What variable are you referring to? – Zerp Jan 02 '15 at 17:49
  • Is referring to int round – AnonymousDoor Jan 02 '15 at 17:53
  • Do not EVER write `new Activity()`. That's not how you start an activity or set up other activities or communicate with new activities. Look at [this](http://stackoverflow.com/questions/3913592/start-an-activity-with-a-parameter) and [that](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) If it makes no sense then I recommend you pick up a book on this topic. – Eugen Pechanec Jan 02 '15 at 18:16

1 Answers1

0

Method "Controllo1()" is called only once when the activity gets created.

So when you click true or false (as it seems both OnClickListeners set "round" to "2") nothing happens. The OnClickListener on the buttons stays the same, so the round will always be 2.

If you want to keep this code as it is, call Controllo1() from the OnClickListener. Alternatively set the new OnClickListener from the onClick methods themselves, but that would make your code unreadable.

LuigiPower
  • 1,053
  • 9
  • 20
  • thanks I have recently started programming andorid . I want to ask you just the way I speak ? – AnonymousDoor Jan 02 '15 at 18:08
  • Visto che il codice e' in italiano scrivo in italiano. Cosa intendi con quella domanda? – LuigiPower Jan 02 '15 at 18:10
  • intendo che dato che sono alle prime armi con la programmazione – AnonymousDoor Jan 02 '15 at 18:16
  • voglio chiederti come ragiono bene o male? altra cosa per favore riscrivi la risposta in italiano che google traduttore non mi fa capire bene cio che vuoi dire in cosa sbaglio inserisci qua come commento – AnonymousDoor Jan 02 '15 at 18:17
  • Controllo1() viene chiamato solo una volta quando viene creata l'Activity. Quindi gli OnClickListener non vengono mai cambiati. Il problema di questo codice e' che hai riscritto piu' volte lo stesso codice, rendendolo un poco illeggibile. Magari rendilo un po' piu' generale, ad esempio con un metodo "prossimo round" che e' l'unico metodo che incrementa la variabile "round" e si occupa di modificare l'interfaccia di conseguenza. – LuigiPower Jan 02 '15 at 18:22
  • ah ho capito grazie allora ora lo sistemo e vedo se va grazie! – AnonymousDoor Jan 02 '15 at 18:25
  • se ho qualche problema richiedo a te grazie! – AnonymousDoor Jan 02 '15 at 18:27