Domanda

I am making a game like logo quiz but I haven't been able to make the player gain score after he answers correctly.

Here is what I have until know:

public class Capitolio extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guess1);
        init();

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true); 
    }

    public boolean onOptionsItemSelected(MenuItem item){
        Intent myIntent = new Intent(getApplicationContext(), Level1.class);
        startActivityForResult(myIntent, 0);
        return true;

    }


    private Button buttonSaveMem1;
    private EditText escrive; 
    private TextView respuest, otra;
    private String [] answers;
    int Score = 0;

    private int currentQuestion;



    public void init() {

        answers = new String[]{"nnnnn"};      
        buttonSaveMem1 = (Button)findViewById(R.id.button1);     
        respuest = (TextView) findViewById(R.id.textView2); 
        otra = (TextView) findViewById(R.id.textView1);
        escrive = (EditText) findViewById(R.id.editText1);
        buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener);

        LoadPreferences();
    }

    Button.OnClickListener buttonSaveMem1OnClickListener = new Button.OnClickListener() {

        @Override    
        public void onClick(View arg0) {    
            checkAnswer();  
            // TODO Auto-generated method stub
            SavePreferences("MEM1", respuest.getText().toString());
            LoadPreferences();
        }
    };

    public boolean isCorrect(String answer) {     
        return (answer.equalsIgnoreCase(answers[currentQuestion]));
    } 

    /* this method :     
    * 1: Read the text ( answer) from the answerTextEdit  
    * 2: call the isCorrect method     
    * 3: display the appropriate message.    
    */ 

    public void checkAnswer()  {     
        String answer = escrive.getText().toString();  
        if(isCorrect(answer))      
            respuest.setText("You're right!" + "The Answer is The Vatican");  
        else     
            respuest.setText("Sorry, The answer is not right!");   
    }

    public void showscore() {     
        String answer = escrive.getText().toString();  
        if(isCorrect(answer))      
            Score += 1; 

        otra.setText(Score);   
    }

    private void SavePreferences(String key, String value) {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    private void LoadPreferences() {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String strSavedMem1 = sharedPreferences.getString("MEM1", "");

        respuest.setText(strSavedMem1);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

And after this I want to develop a way to unlock other levels with this score, but I don't have the knowledge of how to do this.

Thanks for the help.

È stato utile?

Soluzione

You might try something like this:

public void checkAnswer()  {     
    String answer = escrive.getText().toString();  
    if(isCorrect(answer)) {
        respuest.setText("You're right!" + "The Answer is The Vatican");
        Score += 1;
    }
    else {
        respuest.setText("Sorry, The answer is not right!");
    }

    showScore();
}

public void showScore() {     
    otra.setText(Score);   
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top