Switch case statement inside if statement doesn't show multiple choice - android

I cannot get the four multiple choice buttons and the question textView to display text. What happens is there is an intent transferred over with a selected category "who" or "what". That category is compared with the "who" and "what" if statements. If it is one of them then it is supposed to post the question and run through the ten question. I am using a switch case inside the if statement to help distinguish what button was clicked and move on. When I run the emulator it gets to the screen with four multiple choice buttons but nothing is display on the text or text view.
public class QuestionActivity extends AppCompatActivity {
List<QuestionsTable> whoQuesList;
List<QuestionsTable> whatQuesList;
int score=0;
int qid=0;
QuestionsTable currentWhatQ;
QuestionsTable currentWhoQ;
Button btnA, btnB, btnC, btnD;
Button butSkip;
TextView txtQuestion;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
butSkip = (Button) findViewById(R.id.btnSkip);
MySQLiteHelper db = new MySQLiteHelper(this);
whoQuesList = db.getAllWhos();
whatQuesList = db.getAllWhos();
currentWhatQ = whatQuesList.get(qid);
currentWhoQ = whoQuesList.get(qid);
}
public void onClickQuestion(View txtViewQuestion) {
Bundle d = getIntent().getExtras();
int c = d.getInt("category");
String category = Integer.toString(c);
if ("Who".equals(category)) {
txtQuestion = (TextView) findViewById(R.id.txtViewQuestion);
btnA = (Button) findViewById(R.id.btnOptionA);
btnB = (Button) findViewById(R.id.btnOptionB);
btnC = (Button) findViewById(R.id.btnOptionC);
btnD = (Button) findViewById(R.id.btnOptionD);
setQuestionView();
switch (txtViewQuestion.getId()) {
case btnOptionA:
if (currentWhoQ.getAnswer().equals(btnA.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case btnOptionB:
if (currentWhoQ.getAnswer().equals(btnB.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case btnOptionC:
if (currentWhoQ.getAnswer().equals(btnC.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case R.id.btnOptionD:
if (currentWhoQ.getAnswer().equals(btnD.getText())) {
score++;
}
if (qid < 10) {
currentWhoQ = whoQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
}
}
if ("What".equals(category)) {
MySQLiteHelper db = new MySQLiteHelper(this);
whoQuesList = db.getAllWhos();
whatQuesList = db.getAllWhos();
currentWhatQ = whatQuesList.get(qid);
currentWhoQ = whoQuesList.get(qid);
txtQuestion = (TextView) findViewById(R.id.txtViewQuestion);
btnA = (Button) findViewById(R.id.btnOptionA);
btnB = (Button) findViewById(R.id.btnOptionB);
btnC = (Button) findViewById(R.id.btnOptionC);
btnD = (Button) findViewById(R.id.btnOptionD);
setWhatQuestionView();
switch (txtViewQuestion.getId()) {
case btnOptionA:
if (currentWhatQ.getAnswer().equals(btnA.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case btnOptionB:
if (currentWhatQ.getAnswer().equals(btnB.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case btnOptionC:
if (currentWhatQ.getAnswer().equals(btnC.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
case R.id.btnOptionD:
if (currentWhatQ.getAnswer().equals(btnD.getText())) {
score++;
}
if (qid < 10) {
currentWhatQ = whatQuesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(QuestionActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
}
break;
}
}
}
public void setQuestionView() {
txtQuestion.setText(currentWhoQ.getQuestion());
btnA.setText(currentWhoQ.getMultipleChoiceA());
btnB.setText(currentWhoQ.getMultipleChoiceB());
btnC.setText(currentWhoQ.getMultipleChoiceC());
btnD.setText(currentWhoQ.getMultipleChoiceD());
qid++;
}
public void setWhatQuestionView() {
txtQuestion.setText(currentWhatQ.getQuestion());
btnA.setText(currentWhatQ.getMultipleChoiceA());
btnB.setText(currentWhatQ.getMultipleChoiceB());
btnC.setText(currentWhatQ.getMultipleChoiceC());
btnD.setText(currentWhatQ.getMultipleChoiceD());
qid++;
}

Not Sure, what type of Data your txtViewQuestion is...
The Switch Statement Works perfectly with primitive Data types...
If you try to call the Name of the Button Variable, this will Not Work because a program only works with memory adress, so Java will Not be able to give you the name of your object (if its a Button)
To solve this, you could Pass a Parameter to the Methods (for example int)
0 for the first button
1 for the second button
2 for the third...
And then you could "Switch" this Parameter

Bundle d = getIntent().getExtras();
int c = d.getInt("category");
String category = Integer.toString(c);
if ("Who".equals(category)) {
What is the value of c? How could it ever equal "Who" or "What"?
And like others have said, use the full ID of the buttons in the switch statements.
case R.id.btnOptionA:
instead of
case btnOptionA:

What is btnOptionA, btnOptionB and btnOptionC? txtViewQuestion.getId() return an Integer, in case tag you should use R.id.btnOptionA, R.id.btnOptionB, R.id.btnOptionC...

Related

Transfering data from an activity to another and recomposing the name of varibles

I have menu Menu 1 containing 20 buttons bt1, bt2,..btn when you click a button should go to the main activity Main1 where a title and a text should be displayed. Main1 is extended to Text1 where the titles and texts are.
What's needed is that when you click on button2 for example the Main1 should display Title2 and Text2 and so on.
I did this in the Menu 1:
#Override
public void onClick(View v) {
//do common code here
Bundle bundle1 = new Bundle();
bundle1.putString("somekey1", act1);
Intent i = new Intent(getApplicationContext(), Main1.class);
i.putExtras(bundle1);
startActivity(i);
str= v.getResources().getResourceName(v.getId());
act1= Integer.toString(Integer.parseInt(str.substring(str.indexOf("bt")+2 )));
in the Main1 I did this:
num = Integer.parseInt(getIntent().getExtras().getString("somekey1"));
stringId1 = getResources().getIdentifier("title"+num, "string", getPackageName());
stringId2 = getResources().getIdentifier("text"+num, "string", getPackageName());
if (stringId1 > 0) {
title=getString(stringId1);
text2=getString(stringId2);
}
in the Text1 I did this:
public class Text extends Activity {
public String
title1="some title",
text1="some text",
title2="some title",
text2="some text",
titl3="333",
tex3="kar3",
title4="xxxxx",
text4="xxxxx",
title5="",
text5="",
But all of that doesn't work, and about to shake my head on the wall, as the bundle doesn't transfer the data, and stringId1 = getResources().getIdentifier("title"+num, "string", getPackageName());
also returns 0.
Help please`
Pass the intent as shown below:
Intent intent = new Intent(getApplicationContext(),Main1.class);
intent.putExtra("Key1", "Value1");
intent.putExtra("Key2", "Value2");
startActivity(intent);
And in the oncreate() of your Main1.class :
Intent dataIntent = getIntent();
String value1 = dataIntent.getStringExtra("Key1");
String value2 = dataIntent.getStringExtra("Key2");
Please try the following
#Override
public void onClick(View v) {
//do common code here
Bundle bundle1 = new Bundle();
str= v.getResources().getResourceName(v.getId());
act1= Integer.toString(Integer.parseInt(str.substring(str.indexOf("bt")+2 )));
bundle1.putString("somekey1", act1);
Intent i = new Intent(getApplicationContext(), Main1.class);
i.putExtras(bundle1);
startActivity(i);

passing data from textview of activity 1 through textview of activity2

How to pass value of textview form one activity going to the other activity?
I have a scoring on my game that is shown on a textview and after it increment it will intent to the next activity. but the value of score from the first activity doesn't show on the textview of the second activity.
this is my code for my first activity
final TextView score2 = (TextView) findViewById(R.id.tvscore2);
Button page1 = (Button) findViewById(R.id.btnDog);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText etDog1 = (EditText) findViewById(R.id.etDog);
String Dog = etDog1.getText().toString();
if (Dog.equalsIgnoreCase("dog")) {
global.score += 10;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Correct",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(),
sound1_3pig.class);
startActivityForResult(myIntent, 0);
} else if (global.score <= 0) {
global.score += 0;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Wrong",
Toast.LENGTH_SHORT).show();
} else {
global.score -= 5;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Wrong",
Toast.LENGTH_SHORT).show();
}
}
});
}
I want to display the result of the score activity 1 to the textview of the second activity
this is my second activity
final TextView score1 = (TextView) findViewById(R.id.tvscore1);
Button page1 = (Button) findViewById(R.id.btnCat);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText etCat = (EditText) findViewById(R.id.etCat);
String Cat = etCat.getText().toString();
if (Cat.equalsIgnoreCase("cat")) {
global.score += 10;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Correct",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(),
sound1_3pig.class);
startActivityForResult(myIntent, 0);
finish();
} else if (global.score <= 0) {
global.score += 0;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Wrong",
Toast.LENGTH_SHORT).show();
} else {
global.score -= 5;
score2.setText(String.valueOf(global.score));
Toast.makeText(getApplicationContext(), "Wrong",
Toast.LENGTH_SHORT).show();
}
}
});
}
As your second activity inflates a new layout, you need to explicitly pass your value from the first to the second activity and initialize it's TextView using this value.
Activity 1:
void goToSecondActivity() {
String value = mTextView.getText();
Intent in = new Intent(getActivity(), YourSecondClass.class);
in.putExtra("score", value);
startActivity(in);
}
Activity 2:
void onCreate(Bundle bundle) {
...
String score = getIntent().getStringExtra("score", "No score.");
mTextView.setText(score);
}
Use something like this.
Activity 1:
Intent i = new Intent(getActivity(), Activity2.class);
i.putExtra("Score", 20);
startActivity(i);
Activity 2:
Intent i = getIntent();
int score = i.getIntExtra("Score", 0);
My answer is also similar to what the others have suggested. try this, I have added some extra statements within your code(I presumed you want to send the value of variable score to the next activity, you can replace it with the variable you want to send) :
first activity:
score2.setText(String.valueOf(score));
Toast.makeText(getApplicationContext(), "Correct",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(this,
sound1_3pig.class);
myIntent.putExtra("Score", global.score);
startActivityForResult(myIntent, 0);
second activity:
put this in the onCreate() method of the second activity:
Intent intent = getIntent();
int score = intent.getIntExtra("Score",0);
so now you will get the value of score from the previous activity in the second activity. then you can set it in the textView you want to display it in by calling
textView.setText(score);

I tried to pass the values from one to another page but it doesn't work?

I want to pass the values from one page to another page my code is:
public class main extends Activity implements OnClickListener {
private static final String TAG = "AskMeShowMe2";
private String choice = null;
private String fname = null;
private Menu myMenu = null;
final static int START =0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"Main - onCreate() ...");
setContentView(R.layout.activity_main);
View promptButton = findViewById(R.id.submit_button);
promptButton.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.greetingsRadioGroup);
int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
switch (checkedRadioButton) {
case R.id.mrButton :
choice = "Mr.";
break;
case R.id.mrsButton :
choice = "Mrs.";
break;
case R.id.msButton:
choice = "Ms.";
break;
case R.id.drButton:
choice = "Dr.";
break;
}
switch(v.getId()){
case R.id.submit_button:
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
startActivity(j);
fname=String.valueOf(R.id.firstname);
Intent t = new Intent(this, Info.class);
j.putExtra("fname", fname);
startActivity(t);
break;
}
}
and it other Info.Class :
public class Info extends Activity {
private static final String TAG = "Info";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
Intent myIntent = getIntent();
Bundle b = myIntent.getExtras();
String choice = b.getString("choice");
String fname = b.getString("fname");
Log.i(TAG,"selection: " + choice);
TextView textView = (TextView) findViewById(R.id.showmeText);
textView.append(": " + choice);
TextView textView1 = (TextView) findViewById(R.id.fname);
textView1.append(": " + fname);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
}
when i run this program then my Info.class page doesn't show when i click the submit button,Can you please help me how to fix it?
Change Switch block to following:
switch(v.getId()){
case R.id.submit_button:
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
fname=String.valueOf(R.id.firstname);
j.putExtra("fname", fname);
startActivity(j);
break;
}
As you are starting Activity Info two times, Putting extras to only one intent, which is being started earlier.
use
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
fname=String.valueOf(R.id.firstname);
j.putExtra("fname", fname);
startActivity(j);
instead of
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
startActivity(j);
fname=String.valueOf(R.id.firstname);
Intent t = new Intent(this, Info.class);
j.putExtra("fname", fname);
startActivity(t);
becuase currently you are calling startActivity two times first for intent j and second time for intent t so remove one and put both choice and fname in single intent and call startActivity only once.

Android Code - Radom number generation for calling activity [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random number in a range with Java
How can I generate random number in specific range in Android?
Here is my scenario
From the Mainactivity ; On click of a Button i want to generate a random number from 1 to 4
Based on the output, i want to write an if-else that will call 4 different activities
So on click, if 4 is generated, then call activity 4
Next time 1 can be generated and should call activity 1
and so on ....
Can someone please help me with this code?
myBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Random r = new Random();
int index = r.nextInt(4)+1;
Intent intent;
if (index == 4) {
intent = new Intent(Activity.this, Activity4.class);
}
else if (index == 3) {
intent = new Intent(Activity.this, Activity3.class);
}
else if (index == 2) {
intent = new Intent(Activity.this, Activity2.class);
}
else
intent = new Intent(Activity.this, Activity1.class);
startActivity(intent);
}
});
public void test1(){
Random r = new Random();
int index = r.nextInt(4)+1;
Intent intent = null;
switch(index){
case 1: intent = new Intent(this, Activity1.class);
break;
case 2: intent = new Intent(this, Activity2.class);
break;
case 3: intent = new Intent(this, Activity3.class);
break;
case 4: intent = new Intent(this, Activity4.class);
break;
default:
Log.e("ERROR", "");
return;
}
if(intent != null){
this.startActivity(intent);
}
}

Make randomly generated ImageButtons clickable

I'm having trouble with making my randomly generated image, which is interpreted as a button, become clickable. Each leads to a different activity.
The random images work perfect actually, the only problem it's not clickable.
Here's my Main.java:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final List<String> images = new ArrayList<String>();
for (int i=1; i<=13; i++) {
images.add("img"+i);
}
final Button imgView = (Button)findViewById(R.id.top1);
String imgName = null;
int id = 0;
Collections.shuffle(images, new Random());
imgName = images.remove(0);
imageRandomizer(imgName, id, imgView);
}
public void imageRandomizer(String imgName, int id, final Button imgView) {
id = getResources().getIdentifier(imgName,
"drawable",
getPackageName());
imgView.setBackgroundResource(id);
}
}
On my layout, I specified the id top1 as a Button. So the above code will look up to my drawable images, which have the names img1.jpg, img2.jpg, img3.jpg , until img13.jpg.
Making an ImageButton clickable to one activity without being dependent on the shown random image is easy, I can do it without problem.
But what I wanna make is something like, when img1.jpg is generated, it becomes clickable and leads to Activity1.java, for img2.jpg the intent goes to Activity2.java, etc.
EDIT
#Roflcoptr
Here's my OnClickListener:
private OnClickListener top_listener = new OnClickListener() {
public void onClick(View v) {
switch((Integer) v.getTag()) {
case 1:
Intent aid = new Intent(Main.this, ProjektAID.class);
startActivity(aid);
case 2:
Intent adh = new Intent(Main.this, ProjektADH.class);
startActivity(adh);
case 3:
Intent bos = new Intent(Main.this, ProjektBOS.class);
startActivity(bos);
case 4:
Intent brot = new Intent(Main.this, ProjektBROT.class);
startActivity(brot);
case 5:
Intent care = new Intent(Main.this, ProjektCARE.class);
startActivity(care);
case 6:
Intent caritas = new Intent(Main.this, ProjektCARITAS.class);
startActivity(caritas);
case 7:
Intent doc = new Intent(Main.this, ProjektDOC.class);
startActivity(doc);
case 8:
Intent drk = new Intent(Main.this, ProjektDRK.class);
startActivity(drk);
case 9:
Intent give = new Intent(Main.this, ProjektGIVE.class);
startActivity(give);
case 10:
Intent hive = new Intent(Main.this, ProjektHIV.class);
startActivity(hive);
case 11:
Intent jo = new Intent(Main.this, ProjektJOHANNITER.class);
startActivity(jo);
case 12:
Intent kind = new Intent(Main.this, ProjektKINDERHERZ.class);
startActivity(kind);
case 13:
Intent kult = new Intent(Main.this, ProjektKULTURGUT.class);
startActivity(kult);
}
}
};
and here's the randomizer method:
public void imageRandomizer(String imgName, int id, final Button imgView) {
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
imgView.setTag(new Integer(1)); //example for image 1
if (imgName.equals("img1")) {
imgView.setTag(new Integer(1)); //example for image 1
} else if (imgName.equals("img2")) {
imgView.setTag(new Integer(2));
} else if (imgName.equals("img3")) {
imgView.setTag(new Integer(3));
} else if (imgName.equals("img4")) {
imgView.setTag(new Integer(4));
} else if (imgName.equals("img5")) {
imgView.setTag(new Integer(5));
} else if (imgName.equals("img6")) {
imgView.setTag(new Integer(6));
} else if (imgName.equals("img7")) {
imgView.setTag(new Integer(7));
} else if (imgName.equals("img8")) {
imgView.setTag(new Integer(8));
} else if (imgName.equals("img9")) {
imgView.setTag(new Integer(9));
} else if (imgName.equals("img10")) {
imgView.setTag(new Integer(10));
} else if (imgName.equals("img11")) {
imgView.setTag(new Integer(11));
} else if (imgName.equals("img12")) {
imgView.setTag(new Integer(12));
}
else if (imgName.equals("img13")) {
imgView.setTag(new Integer(13));
}
}
I would use a tag to identify the button. So in your imateRandomizer add a unique ID for each possible Image. I don't know how you can identify the images uniquely, but I'll show the example here for the name:
public void imageRandomizer(String imgName, int id, final Button imgView)
{
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
if (imgName.equals("Name of the Image for your first activity") {
imgView.setTag(new Integer(1)); //example for image 1
} else if (imgName.equals("Name of the Image for your second activity") {
imgView.setTag(new Integer(2));
}
}
And then In your ClickListener you can check which tag the button has:
imgView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch((Integer) v.getTag()) {
case 1: //start Activity 1;
break;
case 2: //start Activity 2;
break;
}
}
});

Categories

Resources