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

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);
}
}

Related

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

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...

how to save the result of a query in a int variable?

Hi I develop an application on android ADT. I am using sqlite to validate the registration. When the user registers are saved in a database and the database I want to check if a record already exists for the activity log is not open. This is the code that I used, not shown error but does not function.
public void onContinue(){
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,
"pacman", null, 1);
SQLiteDatabase bd = admin.getReadableDatabase();
int total;
Cursor c = bd.rawQuery("SELECT * FROM numeros", null);
total = c.getCount();
Intent nu;
if(total == 1){
nu = new Intent (this, ContactListActivity.class);
startActivity(nu);
}
else
nu=new Intent(this, Bienvenido.class);
startActivity(nu);
finish();
}
EDIT2:
I believe you wanted to start 1 activity only but when (total == 1) you are starting 2.
You should change
if(total == 1){
nu = new Intent (this, ContactListActivity.class);
startActivity(nu);
}
else
nu=new Intent(this, Bienvenido.class);
startActivity(nu);
To
if(total == 1)
nu = new Intent (this, ContactListActivity.class);
else
nu = new Intent(this, Bienvenido.class);
startActivity(nu);

passing int into android activities

i pass int to next activity using this code
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType", i);
startActivity(intent);
and then receive this in activity B
Intent intent = new Intent();
int i = intent.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(i),
Toast.LENGTH_LONG).show();
but when in this activity, it always display 0.
Intent intent = new Intent();
You are creating a new intent instead of using the one passed to your ActivityB. So use
Intent intent = getIntent();
instead;
use this int i = getIntent().getIntExtra("selectedType", 0);
try getIntent().getExtras().getInt("selectedType")
Try now,
int value = getIntent().getExtras().getInt("selectedType");
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType", i);
startActivity(intent);
Intent intent = new getIntent();
^^^^^^^^^
int i = intent.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(i),
Toast.LENGTH_LONG).show();
Because you're creating a new intent and trying to get "selectedType" on it. But that intent has just been created, so it hasn't that value that you seek.
Try to getIntent() method to get your calling intent, which has your "selectedType" value...
Here's a snap:
Bundle extras = getIntent().getExtras();
if(extras != null) {
int value = extras.getIntExtra("selectedType", 0);
Toast.makeText(getApplicationContext(), String.valueOf(value), Toast.LENGTH_LONG).show();
}
and then receive this in activity B
Intent intent = new Intent();
int i = intent.getIntExtra("selectedType", 0);
This is wrong. You are creating a new intent object. To get the intent object that was used to start this activity use getIntent() method.
Intent intent = getIntent();
int i = intent.getIntExtra("seelctedType", 0);
Intent intent = new Intent(A.this, B.class);
intent.putExtra("selectedType",i);
startActivity(intent);
And receiving..
if (getIntent().getExtras().containsKey("selectedType")) {
int message = getIntent().getIntExtra("selectedType");
Toast.makeText(ReceiverActivity.this, "" + message, Toast.LENGTH_LONG)
.show();
}

How can I display random activity when click button

I used following code in main activity
public void button(View v){
//Create an intent to start the new activity.
// Our intention is to start secondActivity
Intent intent = new Intent();
intent.setClass(this,Activity.class);
startActivity(intent);
}
How can I display random activity when click button? Please help me!
debugged some codes above because there's an error in setClass().. this one works:
Random rnd = new Random();
int x=rnd.nextInt(3)+1;
Intent myIntent = new Intent();
switch(x){
case 1:
myIntent.setClass(view.getContext(),Scrn1.class);
break;
case 2:
myIntent.setClass(view.getContext(), Scrn2.class);
break;
case 3:
myIntent.setClass(view.getContext(), Scrn1.class);
break;
}
startActivity(myIntent);
Store the name of all your activities in an array, generate random number and get activity corresponding to random generated number. Sample snippet
String[] title = new String[] { "Act1.class", "Act2.class","Act1.class","Act4.class","Act5.class"};
public String getRandomActivity(){
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(5);// pass number of elements as parameters
return title[randomInt-1];//this should return class name
}
Intent intent = new Intent(this,getRandomActivity())
startActivity(intent)
Try this, assume you have 3 activities:
public void button(View v){
Random rnd = new Random();
int x=rnd.nextInt(3)+1;
Intent intent = new Intent();
switch(x){
case 1:
intent.setClass(this,Activity1.class);
break;
case 2:
intent.setClass(this,Activity2.class);
break;
case 3:
intent.setClass(this,Activity3.class);
break;
}
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