7 Replies - 5072 Views - Last Post: 17 December 2011 - 12:34 AM

#1 Medic873  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 16-November 08

android public void access question

Posted 12 December 2011 - 04:50 PM

okay first of all man has it been along time since ive been out here on dream in code and damn it feels good to be back.

But ive recently started developing applications for android and damn its confusing (I dont have a java background more C++ and C#)

But currently my app is set up where it will reference simple public voids for my question data.
	public void Question1(){
		question.setText("How good are you at programming?");
		answer1.setText("A. Amazing.");
		answer2.setText("B. Good.");
		answer3.setText("C. Okay.");
		answer4.setText("D. Horrible.");
		answernumber = 1;
	}



the only thing here that might confuse someone is the answernumber variable witch is suppose to be the answer. (*probably shouldn't be A for me)

but I have about 10 Questions loaded into this app and my only issue is there is a next question button
and what I would like it to do is add 1 to the questioncount variable then call apoun that public void variable

like so although this doesn't work saddly
	public void onclick(View arg0) {
		questioncount ++;
		Question + questioncount();
		
	}




how can i do this in a fashion that will work thank you

Is This A Good Question/Topic? 0
  • +

Replies To: android public void access question

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10784
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: android public void access question

Posted 12 December 2011 - 05:09 PM

Moved to Android.

It would make more sense to design a Question class to encapsulate the question, answers, and identify the correct answer. Then have your app store a List<Question> or Question[]. Having methods (or individual objects) named Question1(), Question2(), etc., is overkill.
Was This Post Helpful? 1
  • +
  • -

#3 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: android public void access question

Posted 12 December 2011 - 07:15 PM

macosxnerd101 is absolutely correct! It makes more sense to create a Question class and crate an array or list of questions!
An example of what the Question calls could look like is:
/**
 *
 * @author Thomas Otero (H3R3T1C)
 * Email: th3h3r3t1c@gmail.com
 */
public class Question {

    private String question;
    private String ans1;
    private String ans2;
    private String ans3;
    private String ans4;
    
    public Question(String q,String a1,String a2,String a3, String a4)
    {
        question = q;
        ans1 = a1;
        ans2 = a2;
        ans3 = a3;
        ans4 = a4;
    }

    public String getAns1() {
        return ans1;
    }

    public void setAns1(String ans1) {
        this.ans1 = ans1;
    }

    public String getAns2() {
        return ans2;
    }

    public void setAns2(String ans2) {
        this.ans2 = ans2;
    }

    public String getAns3() {
        return ans3;
    }

    public void setAns3(String ans3) {
        this.ans3 = ans3;
    }

    public String getAns4() {
        return ans4;
    }

    public void setAns4(String ans4) {
        this.ans4 = ans4;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }
}




My suggestion for putting the data from some source (like a server or file on SDCard) would be to use xml!
Example xml data:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
	<Question ques="what is a question?" ans1="answer 1" ans2="answer 2" ans3="answer 4" ans4="answer 4" />
	...
</Data>


For an example on how to parse xml data into classes check out this thread: http://www.dreaminco...-to-the-sdcard/

This post has been edited by H3R3T1C: 12 December 2011 - 07:15 PM

Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10784
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: android public void access question

Posted 12 December 2011 - 07:34 PM

This still begs for an array or List, though. That would make it a lot more modular and reusable, especially if you want to add multiple answers later.
private String ans1;
private String ans2;
private String ans3;
private String ans4;


Was This Post Helpful? 0
  • +
  • -

#5 Medic873  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 16-November 08

Re: android public void access question

Posted 12 December 2011 - 10:36 PM

okay that is all good and im trying to get it to work but for the time being is there any way to do what i was saying and call apon

PUBLIC VOID NAME + VARIBLE

like

Question + QuestionCount();


and thank you for the extream amount of help
Was This Post Helpful? 0
  • +
  • -

#6 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: android public void access question

Posted 12 December 2011 - 10:45 PM

Sorry but there is no way to do the name of a method and a variable like you want!
The way I suggested above is way more efferent then having a method for each question.
One thing you could do but I dont suggest you do for the final version of the app but only use for simple testing would be to use switch case like so:
private void nextQuestion(int i)
{
    switch(i)
	{
	    case 0:
		{
			question1();// call to question one method
			break;
		}
		case 1:
		{
			question2();// call to question two method
			break;
		}
		...
	}		
}


This post has been edited by H3R3T1C: 12 December 2011 - 10:47 PM

Was This Post Helpful? 0
  • +
  • -

#7 Medic873  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 16-November 08

Re: android public void access question

Posted 13 December 2011 - 12:14 AM

okay may I start by saying thank you for all the help all of this is greatly appreciated and I know I might be slow but im new to this language and this is a great learning expreience for me.

But I understand what you guys are trying to say but im complete lost on the method at witch to do it.

So ive decided it might be easier for me to go another route and show you my code so you might see it through my eyes possible help me along in setting this up. (Examples really help so thank you)

But ill post both my xml and activity code

XML CODE
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/TQ"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How far must you stop before a stop sign?"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/A1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A: 300 Feet" />

        <RadioButton
            android:id="@+id/A2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="B: 200 Feet" />

        <RadioButton
            android:id="@+id/A3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C: 500 Feet" />

        <RadioButton
            android:id="@+id/A4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="D: 50 Feet" />
    </RadioGroup>

</LinearLayout>



ACTIVITY CODE
package dmv.quiz;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class DMVQuizActivity extends Activity implements OnCheckedChangeListener {
	
	int answernumber, questioncount;
	TextView question, answer1, answer2, answer3, answer4;
	RadioGroup radioGroup;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		question = (TextView) findViewById(R.id.TQ);
		answer1 = (TextView) findViewById(R.id.A1);
		answer2 = (TextView) findViewById(R.id.A2);
		answer3 = (TextView) findViewById(R.id.A3);
		answer4 = (TextView) findViewById(R.id.A4);
		answernumber = 2;
		questioncount = 1;
		radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
		radioGroup.setOnCheckedChangeListener(this);
	}
	
	public void AZQuestion1(){
		question.setText("How old must you be to obtain a state class C driver license?");
		answer1.setText("A. 12 Years Old");
		answer2.setText("B. 16 Years Old");
		answer3.setText("C. 10 Years Old");
		answer4.setText("D. 18 Years Old");
		answernumber = 2;
	}
	public void AZQuestion2(){
		question.setText("When you approach an intersection on a main road, and the intersection is blocked with traffic. You should?");
		answer1.setText("A. Sound horn to warn cars ahead to move on.");
		answer2.setText("B. Proceed slowly into the intersection until the traffic ahead moves on.");
		answer3.setText("C. Stop before entering the intersection and wait until traffic ahead moves on.");
		answer4.setText("D. Keep as close as possible to the car ahead");
		answernumber = 3;
	}
	public void AZQuestion3(){
			question.setText("A person whose driver license is under suspension, may?");
			answer1.setText("A. Operate a motor vehicle when accompanied by a licensed driver.");
			answer2.setText("B. Operate a motor vehicle to and from work.");
			answer3.setText("C. Operate a motor vehicle in a case of extreme emergency.");
			answer4.setText("D. Not drive, under any circumstances.");
			answernumber = 4;
		}
	public void AZQuestion4(){
		question.setText("When you see a vehicle stopped on the right shoulder of the road ahead with its hazard lights on?");
		answer1.setText("A. Don't change lane but speed up.");
		answer2.setText("B. Slow down and pass with caution.");
		answer3.setText("C. Stop your vehicle until you can see what happened.");
		answer4.setText("D. Change lane to the left and speed up.");
		answernumber = 2;
	}
	public void AZQuestion5(){
		question.setText("A motorist approaching a bicyclist should?");
		answer1.setText("A. Speed up to pass him.");
		answer2.setText("B. Proceed as usual.");
		answer3.setText("C. Exercise extreme caution.");
		answer4.setText("D. Swerve into the opposite lane.");
		answernumber = 3;
	}
	public void AZQuestion6(){
		question.setText("When coming to an intersection where you want to turn right but there is a bicyclist ahead of you, what should you do?");
		answer1.setText("A. All answers are correct.");
		answer2.setText("B. Let the bicycle go ahead of you and then turn right.");
		answer3.setText("C. Pass the bicycle and quickly turn right.");
		answer4.setText("D. Pass the bicycle because you have the right of way.");
		answernumber = 2;
	}
	public void AZQuestion7(){
		question.setText("When are you allowed to pass a school bus?");
		answer1.setText("A. If the school bus has stopped, and red lights are flashing, and you are traveling in the same direction as the bus.");
		answer2.setText("B. If the school bus has stopped, and red lights are flashing, but the bus is traveling in the opposite direction.");
		answer3.setText("C. If the school bus has stopped, and red lights are flashing, but no students present.");
		answer4.setText("D. None of the above.");
		answernumber = 4;
	}
	public void AZQuestion8(){
		question.setText("When entering a freeway?");
		answer1.setText("A. You have the right of way.");
		answer2.setText("B. Freeway vehicles have the right of way.");
		answer3.setText("C. Large truck have right of way.");
		answer4.setText("D. Small cars have right of way.");
		answernumber = 2;
	}
	public void AZQuestion9(){
		question.setText("If your vehicle breaks down on a highway. What should you do?");
		answer1.setText("A. Have all occupants stay inside the vehicle.");
		answer2.setText("B. Try to do roadside repairs.");
		answer3.setText("C. You should leave your vehicle and call the police.");
		answer4.setText("D. You should warn other road users that your vehicle is there with emergency flashers.");
		answernumber = 4;
	}
	public void AZQuestion10(){
		question.setText("When a traffic signal is showing a flashing yellow light, you must?");
		answer1.setText("A. Slow down before entering the crosswalk or intersection, and be especially alert.");
		answer2.setText("B. Stop completely before entering the crosswalk or intersection, and remain stopped until a green signal appear.");
		answer3.setText("C. Slow down before entering the intersection, but only if you can do so safely.");
		answer4.setText("D. Stop completely before entering the crosswalk or intersection, only proceed when you can do so safely.");
		answernumber = 1;
	}

	public void Correctlyanswered() {
		Toast Correct = Toast.makeText(DMVQuizActivity.this, "Correct!",
		Toast.LENGTH_SHORT);
		Correct.show();
		radioGroup.clearCheck();
		
		
		
		
	}
	public void Incorrectlyanswered() {
		Toast Incorrect = Toast.makeText(DMVQuizActivity.this,
				"Sorry please try agian!", Toast.LENGTH_SHORT);
		Incorrect.show();
	}

	public void onCheckedChanged(RadioGroup arg0, int arg1) {
		switch (arg1) {
		case R.id.A1:
			if (answernumber == 1){
				Correctlyanswered();
			}else{
				Incorrectlyanswered();
			}
			break;
		case R.id.A2:
			if (answernumber == 2){
				Correctlyanswered();
			}else{
				Incorrectlyanswered();
			}
			break;
		case R.id.A3:
			if (answernumber == 3){
				Correctlyanswered();
			}else{
				Incorrectlyanswered();
			}
			break;
		case R.id.A4:
			if (answernumber == 4){
				Correctlyanswered();
			}else{
				Incorrectlyanswered();
			}
			break;
		}
	}


}



Thank you
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10784
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: android public void access question

Posted 17 December 2011 - 12:34 AM

H3R3T1C has already shown you what a Question class could look like, so let's go from there.

I've set up a basic skeleton class with the array and index, along with lots of comments with the logic for you to fill in. I also have a tutorial Moving Away From Parallel Arrays, which covers basic OOP.
class MyActivity extends Activity implements OnCheckedChangeListener{

     private Question[] questions; //track the questions
     private int index;  //track the index of the current Question
  
     public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);

         questions = new Question[10]; 

         //now here, you will want to populate the array with
         //Question objects in each element

         index = 0; //we currently have no questions answered

         //now here, set up your UI Components to display 
         //the question text and answers from questions[0]        
     }

     public void onCheckedChanged(RadioGroup arg0, int arg1){
          //now in this method, you will want to start by checking the answer
          //note that questions[index] holds the correct answer, so you will have
          //to get it from questions[index]. From there, you will have to increment 
          //index and update the UI to display the appropriate values from the next 
          //Question object
     }
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1