1. Download our Official Android App: Forums for Android!

Apps Call a method in another class

Discussion in 'Android Development' started by Slaine82, Aug 8, 2011.

  1. Slaine82

    Slaine82 Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    10
    Joined:
    Aug 1, 2011

    Aug 1, 2011
    10
    0
    15
    Software Developer
    Dublin, Ireland
    Hi there,

    I am building a quiz application and I have two classes:-

    Main class which holds the menu for the application and the main parts of the code
    Question class which creates and sets up a question, and hold methods for evaluating the questions answer

    The main class has a method public void setQuestion(int qNo) which creates a new Question object. The thing is once I have created the question and evaluated it which happens in the Question class, from this class I want to call the setQuestion method back in the Main class, is this possible?

    Below are my two classes:-

    MAIN

    Code (Text):
    1.  
    2. package com.zapp.potogold;
    3.  
    4. import android.app.Activity;
    5. import android.content.Context;
    6. import android.content.res.Resources;
    7. import android.os.Bundle;
    8. import android.view.View;
    9. import android.widget.Button;
    10. import android.widget.Toast;
    11.  
    12. public class Main extends Activity {
    13.     int currentQ = 0;
    14.     Context currentContext;
    15.     Question question;
    16.     Button submitButton;
    17.     Resources res;
    18.  
    19.    
    20.     /** Called when the activity is first created. */
    21.     @Override
    22.     public void onCreate(Bundle savedInstanceState) {
    23.         super.onCreate(savedInstanceState);
    24.         setContentView(R.layout.main);
    25.        
    26.         currentContext = this;
    27.     }
    28.    
    29.    
    30.     public void setView(View view){
    31.        
    32.         switch(view.getId()){
    33.  
    34.           case R.id.menuitem_1:
    35.               //activate the quiz layout
    36.               setContentView(R.layout.quiz);
    37.               setQuestion(currentQ);
    38.               break;
    39.                
    40.           case R.id.menuitem_2:
    41.  
    42.               break;
    43.           }
    44.  
    45.     }
    46.    
    47.     public void setQuestion(int qNo){
    48.         submitButton = (Button) findViewById(R.id.submit);
    49.         submitButton.setEnabled(false);
    50.         submitButton.setOnClickListener(new View.OnClickListener() {
    51.             public void onClick(View v) {
    52.                 submitClicked();
    53.             }
    54.         });
    55.         question = new Question(currentContext, qNo);
    56.     }
    57.    
    58.     public void submitClicked(){
    59.         question.evaluateAnswer();
    60.     }
    61. }
    62.  
    QUESTION

    Code (Text):
    1.  
    2. package com.zapp.potogold;
    3.  
    4. import android.app.Activity;
    5. import android.content.Context;
    6. import android.content.res.Resources;
    7. import android.view.View;
    8. import android.view.View.OnClickListener;
    9. import android.widget.Button;
    10. import android.widget.RadioButton;
    11. import android.widget.RadioGroup;
    12. import android.widget.TextView;
    13.  
    14. public class Question {
    15.     private Context mContext;
    16.     private TextView tv;
    17.     Resources res;
    18.     RadioButton radioButton;
    19.     RadioButton selectedRadioButton;
    20.     RadioGroup rg;
    21.     Button submitButton;
    22.     Button nextQButton;
    23.     int currentQNo;
    24.     static int quizPoints = 0;
    25.    
    26.     //constructor
    27.     public Question(Context ctx, int qNumber) {
    28.         mContext = ctx;
    29.         setQStem(qNumber);
    30.         setAnswers(qNumber);
    31.         submitButton = (Button) ((Activity) mContext).findViewById(R.id.submit);
    32.         currentQNo = qNumber;
    33.     }
    34.    
    35.     public void setQStem(int qNumber){
    36.         //set the question text
    37.         tv = (TextView)((Activity) mContext).findViewById(R.id.questionView);
    38.        
    39.         res = mContext.getResources();
    40.        
    41.         String[] questions = res.getStringArray(R.array.questions);
    42.         String qText = questions[qNumber];
    43.  
    44.         tv.setText(qText);
    45.     }
    46.    
    47.     public void setAnswers(int qNumber){
    48.         String[] answers = res.getStringArray(R.array.qAnswers);
    49.         String currentQAnswers = answers[qNumber];
    50.         String[] separated = currentQAnswers.split("@");
    51.         rg = (RadioGroup)((Activity) mContext).findViewById(R.id.radioGroup1);
    52.         for(int a = 0; a < separated.length; a++){
    53.             radioButton = new RadioButton(mContext);
    54.             radioButton.setText(separated[a]);
    55.             radioButton.setId(a);
    56.             rg.addView(radioButton);
    57.             radioButton.setOnClickListener(radio_listener);
    58.         }
    59.     }
    60.    
    61.     public void evaluateAnswer(){
    62.         String[] correctAnswers = res.getStringArray(R.array.correctAnswers);
    63.         String currentQCorAns = correctAnswers[currentQNo];
    64.         String currentSelectedString = (String)(selectedRadioButton.getText());
    65.        
    66.         if(currentSelectedString.equals(currentQCorAns)){
    67.             quizPoints += 2;
    68.             setResults("correct");
    69.         } else {
    70.             setResults("incorrect");
    71.         }
    72.     }
    73.    
    74.     private OnClickListener radio_listener = new OnClickListener() {
    75.         public void onClick(View v) {
    76.             submitButton.setEnabled(true);
    77.             // Perform action on clicks
    78.             selectedRadioButton = (RadioButton) v;
    79.         }
    80.     };
    81.    
    82.     private void setResults(String result){
    83.         ((Activity) mContext).setContentView(R.layout.results);
    84.         String nextText = ((Activity) mContext).getString(R.string.result_next);
    85.        
    86.         TextView correctTextView = new TextView(mContext);
    87.         TextView scoreTextView = new TextView(mContext);
    88.         TextView nextTextView = new TextView(mContext);
    89.  
    90.         correctTextView = (TextView)((Activity) mContext).findViewById(R.id.resultText1);
    91.         scoreTextView = (TextView)((Activity) mContext).findViewById(R.id.resultScore);
    92.         nextTextView = (TextView)((Activity) mContext).findViewById(R.id.resultText2);
    93.        
    94.         nextTextView.setText(nextText);
    95.         scoreTextView.setText(Integer.toString(quizPoints));
    96.         nextTextView.setText(nextText);
    97.  
    98.         if(result == "correct"){
    99.             String correctText = ((Activity) mContext).getString(R.string.result_correct);
    100.             correctTextView.setText(correctText);
    101.         } else {
    102.             String incorrectText = ((Activity) mContext).getString(R.string.result_incorrect);
    103.             correctTextView.setText(incorrectText);
    104.         }
    105.        
    106.         nextQButton = (Button)((Activity) mContext).findViewById(R.id.nextQButton);
    107.         nextQButton.setOnClickListener(new View.OnClickListener() {
    108.             public void onClick(View v) {
    109.                 [B]//I want to call the setQuestion function in the main class to call the next question[/B]
    110.             }
    111.         });
    112.     }
    113.    
    114. }
    115.  
    and I have bolded where I want to call the function from the Question class, any help would be much appreciated.
     

    Advertisement

  2. redcrab

    redcrab Newbie
    Rank:
    None
    Points:
    36
    Posts:
    14
    Joined:
    Aug 9, 2011

    Aug 9, 2011
    14
    4
    36
    your Main Class is Your Activity that is a Context.. and I guess that you may use your Main.this as context into your Question class that is referenced therefore after by it mContext field member

    so you may cast mContext as Main and use your method inside your question class


    ((Main)mContext).setQuestion(......);

    hope that help


    have fun
     
    Slaine82 likes this.
  3. Slaine82

    Slaine82 Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    10
    Joined:
    Aug 1, 2011

    Aug 1, 2011
    10
    0
    15
    Software Developer
    Dublin, Ireland
    Thanks Redcrab!
     

Share This Page

Loading...