how to shuffle Images in Android - android

In project 1 question & Its 4 Answer .
Here As question I want to take 1 image Randomaly
& Its Answer 4 images Randomaly.
But Problem is that which image As question me take Randomaly
It also want to take in Answer 4 images also contain question image
how to this possible
Pls Reply

Here top is the Question & col1,col2,col3,col4
this r the Answers which r the Randomaly come
int num is the totally questions & d Answers
int top,col1,col2,col3,col4,num=8;
top=(int)Math.floor(Math.random()*num);
col1=(int)Math.floor(Math.random()*num);
col2=(int)Math.floor(Math.random()*num);
col3=(int)Math.floor(Math.random()*num);
col4=(int)Math.floor(Math.random()*num);
After this int convert to String
String topstr,col1str,col2str,col3str,col4str;
topstr=String.valueOf(top);
col1str=String.valueOf(col1);
col2str=String.valueOf(col2);
col3str=String.valueOf(col3);
col4str=String.valueOf(col4);
check condition through if loop
here Imageview imgtopcolor,imgcolortap1,imgcolortap2,imgcolortap3,imgcolortap4;
here int Toppickid[]=new int[num];
int Colpickid[]=new int[num];
Toppickid[0]=R.drawable.img0;
//...
Toppickid[7]=R.drawable.img7;
Same as Colpickid[]
if(topstr.equalsIgnoreCase(col1str) || topstr.equalsIgnoreCase(col2str) || topstr.equalsIgnoreCase(col3str) || topstr.equalsIgnoreCase(col4str))
{if(!col1str.equalsIgnoreCase(col2str) )
{ if(!col1str.equalsIgnoreCase(col3str))
{if(!col1str.equalsIgnoreCase(col4str))
{if( !col2str.equalsIgnoreCase(col3str) )
{if( !col2str.equalsIgnoreCase(col4str))
{if( !col3str.equalsIgnoreCase(col4str))
{
imgtopcolor.setImageResource(Toppickid[top]);
imgcolortap1.setImageResource(Colpickid[col1]);
imgcolortap2.setImageResource(Colpickid[col2]);
imgcolortap3.setImageResource(Colpickid[col3]);
imgcolortap4.setImageResource(Colpickid[col4]);
} } } } } } } }

This link here might be help..
It contains shuffle of 4 images and one text question. You can add the question with image.

Related

Can I create an array of song and use the shuffle() method for it? [duplicate]

This question already has answers here:
Random shuffling of an array
(31 answers)
Closed 4 years ago.
Hi can I have and array of int[] music = {R.raw.song1, R.raw.song2}
and use the music.shuffle() ? so that I will put this method in the next button for it to shuffles to a random songs ?
hope this helps
private int getRandomSong(int[] music) {
int max = music.length - 1;
int min = 0;
int random = (new Random()).nextInt((max - min) + 1) + min;
return music[random];
}
happy coding :)
next time try to give a bit more information about the question else you will get down votes unnecessarily :)

Implementing fifo in an array

I have an array and I want to implement FIFO. my project consist of 5 games and a score for each individual game. I had already coded it. heres my code:
if (Game.isNewScore) {
for (int i = 0; i < 5; i++) {
if (addition[i] == 0) {
addition[i] = Game.score;
break;
}
}
}
now my problem is when Im already in the sixth game the scoring should replace the score in the 1st game then transfering the scores from left but it seems the score from the game 6 is not showing.
e.g
2 4 6 8 9 - scores
1 2 3 4 5 - no. of Games
when on sixth game
4 6 8 9 (new score)
can someone teach me on this kind of logic? when you seems not getting my point please tell me. Im badly needing a help. thanks
Your best for this would be to change addition for int[] to ArrayList<Integer>. Your code could look something like this then :
ArrayList<Integer> addition = new ArrayList<>();
/*some other code....
*/
if(Game.isNewScore) {
if (addition.size() == 5){
addition.remove(0);
}
addition.add(Game.score);
}

android apps using if else statement with multiple condition

Hye, I have a problem and a bit confuses to solve it… I need to create a checklist for user to answer in my android apps..it contain several questions…for example, I have 5 questions and user need to answer by choosing the options on radiobutton…So basically this checklist is to check if the user is allowed to drive a car or not..
1) Are you under influence of drugs? //RG1, rbyes1, rbno1
O Yes O No
2) Do you have driving license? //RG2, rbyes2, rbno2
O Yes O No
3) Do you have a car? //RG3, rbyes3, rbno3
O YES O No
4) Are you drunk? //RG4, rbyes4, rbno4
O Yes O No
5) Are you colour blind? //RG5, rbyes5, rbno5
O Yes O No
User need to answer
1) No
2) Yes
3) Yes
4) No
5) No
To make the user eligible to drive a car..
If user answer the opposite, system should display the reason for every opposite answer..
This is my sample code
If(RG1. getCheckedRadioButtonId() == R.id.rbno1 &&
RG2. getCheckedRadioButtonId() == R.id.rbyes2 &&
RG3. getCheckedRadioButtonId() == R.id.rbyes3 &&
RG4. getCheckedRadioButtonId() == R.id.rbno4 &&
RG5. getCheckedRadioButtonId() == R.id.rbno5)
{
Toast.makeText(getApplicationContext(),”Congratz, you can drive”, Toast.LENGTH_LONG).show();
}
Above code is user answers for the eligible one..what if the user answer like this
1) No
2) No
3) Yes
4) Yes
5) No
So, system should display
{
Toast.makeText(getApplicationContext(),”Sorry, you are not allow to drive because you have\n“+
“2) No driving license\n” +
“4) influenced of alcohol\n”, Toast.LENGTH_LONG).show();
}
This is a piece of my idea for what I can do..Do I need to create every possibilities???.. because for the actual one, I got more than 10 questions… So, that’s means there will have many condition.. and I also confuse how to write it in code because of too many condition..hopefully you guys understand what I means and can help me…thank you..!
You do not need to check for every possibility. You check every answer only once and keep track of the error messages, e.g.:
boolean canDrive = true;
ArrayList<String> errorMessages = new ArrayList<String>();
if(RG1.getCheckedRadioButtonId() == R.id.rbno1) {
canDrive = canDrive && true;
} else {
canDrive = false;
errorMessages.add("Influenced of drugs");
}
if(RG2.getCheckedRadioButtonId() == R.id.rbyes2) {
canDrive = canDrive && true;
} else {
canDrive = false;
errorMessages.add("No driving license");
}
// Check the rest of your answers
At the end of your conditions you will have the variable canDrive reflects the result of the user answers and you will have all the error messages in errorMessages list which you can loop through and display appropriately.
Try to make your code more generic, consider using Array of your CheckedRadioButtons and you can store the error message for each question using checkBox.setTag()
Solution 1:
In my opinion you have to create on Database for storing the result.
When you have to require print the error message that time you have to fire on simple select query with result yes.
then print your message
Solution 2:
You have to create arrayList and store result value if yes.
Then you have to find length of arrayList and put your condition and put message

Loop structure gives wrong result

I am trying to compare items out of my DB to the value of an EditText (user input). The answer can have multiple answers, seperated by a ','. I first put them into a stringarray and then compare them to the answer. The LevenshteinDistance checks if the answer is more or les good (http://en.wikipedia.org/wiki/Levenshtein_distance#Computing_Levenshtein_distance).
userAnswer = etUserAnswer.getText().toString().toLowerCase();
String[] answers = qAnswer.split(",");
for (String answer : answers) {
if (answer.equals(userAnswer)) {
Toast.makeText(getApplicationContext(), ("Answer Correct"),
Toast.LENGTH_SHORT).show();
tvMessage.setText("You smartass!");
} else {
Toast.makeText(getApplicationContext(), ("Wrong"),
Toast.LENGTH_SHORT).show();
points = points - 4;
String answerGood = answer.toLowerCase();
LevenshteinDistance lDistance = new LevenshteinDistance();
int comparisonCheck = lDistance.computeLevenshteinDistance(
userAnswer, answerGood);
if (comparisonCheck == 1) {
tvMessage.setText("Almost there, but not quite yet!");
} else if (comparisonCheck > 1) {
tvMessage.setText("Are you serious, totally wrong?!");
}
}
}
Suppose I am having the answers for a question in the DB as follows: tree,test,radio
I am having two problems:
1. When I type "radi" it gives me 'Almost there...' which is good. It should also give me this if I enter "tes", but instead it gives me the 'Are you serious,...' line. I guess it keeps comparing to the last one.
2. Every time I type in something which is not correct, I get -12 instead of -4. I suppose this is due to the fact I am having three answers and it loops three times.. but I don't know how I can make it count only once..
Anyone can help me on the way? Thanks!
Assuming you don't need to know the word which gives the least Levenshtein distance, you could modify your loop to find smallest distance only;
userAnswer = etUserAnswer.getText().toString().toLowerCase();
String[] answers = qAnswer.split(",");
LevenshteinDistance lDistance = new LevenshteinDistance();
int minDistance = lDistance.computeLevenshteinDistance(
userAnswer, answers[0].toLowerCase());
for (int i = 1; i < answers.length; ++i) {
minDistance = Math.min(minDistance, lDistance.computeLevenshteinDistance(
userAnswer, answers[i].toLowerCase()));
}
if (minDistance == 0) {
// Correct answer...
} else {
// Wrong answer...
points -= 4;
// etc etc...
}

How set selection in spinner of some string if spinner contains the same? [duplicate]

This question already has answers here:
How to set selected item of Spinner by value, not by position?
(25 answers)
Closed 8 years ago.
I posted similar question recently but nobody helped me. So I'll try to explain my problem better than before.
So I read some text from one file (this file may include more words) and then I read text from second file (this file always includes one word which is the same as one word in first file). Text in both files may be different everytime.
So for example:
First string contains: black blue yellow red green
Second string contains: yellow
Then I make spinner from first string, so spinner contains in this example these words (black blue yellow red green), so default option is black (coz it is first in my array), but I need to make third position as default in my spinner in this example, because second string contains yellow and yellow is on the third position in my spinner.
How can I make it without repopulating spinner?
btw. these strings are only example. Files may always includes different words than before.
Solution:
s1.setSelection(getIndex(s1, prefNameCurGOV));
and then:
private int getIndex(Spinner s1, String prefNameCurGOV){
int index = 0;
for (int i=0;i<s1.getCount();i++){
if (s1.getItemAtPosition(i).equals(prefNameCurGOV)){
index = i;
}
}
return index;
Something like:
String secondString = secondSpinner.getSelectedItem();
firstSpinner.setSelection(getIndex(firstSpinner,secondString));
then use
private int getIndex(Spinner spinner,String string){
//Pseudo code because I dont remember the API
int index = 0;
for (int i = 0; i < firstSpinner.size(); i++){
if (firstSpinner.getItemAtPosition(i).equals(string)){
index = i;
}
}
return index;
}

Categories

Resources