Issue
I have two activities. One activity shows a set of random images from an array of images. Then after 10 seconds, the second activity shows a set of clickable random images. The objective is to make the user click the images common to both the activities. The app should show the number of correct clicks and wrong clicks on a textview. I tried to compare the drawables of the two activities but with no luck. Please suggest me a way to get out of this problem. Getting the resource ID of the clicked images would help. But I was not able to find anything that would help me get that.I am fairly new to Android.
MainActivity.java:
public class MainActivity extends Activity
{
int i=5;
static int[] imageViews ={
R.id.imageView1,R.id.imageView2,R.id.imageView3,
R.id.imageView4,R.id.imageView5,R.id.imageView6,
R.id.imageView7,R.id.imageView8,R.id.imageView9,
R.id.imageView10,R.id.imageView11,R.id.imageView12,
R.id.imageView13,R.id.imageView14,R.id.imageView15,
R.id.imageView16,R.id.imageView17,R.id.imageView18
};
static int[] images = {
R.drawable.animalsimg1,R.drawable.animalsimg2,R.drawable.animalsimg3, R.drawable.animalsimg4,R.drawable.animalsimg5,R.drawable.animalsimg6,
R.drawable.animalsimg7,R.drawable.animalsimg8,R.drawable.animalsimg9,
R.drawable.animalsimg10,R.drawable.animalsimg11,R.drawable.animalsimg12,
R.drawable.animalsimg13,R.drawable.animalsimg14,R.drawable.animalsimg15,
R.drawable.animalsimg16,R.drawable.animalsimg17,R.drawable.animalsimg18,
R.drawable.animalsimg19,R.drawable.animalsimg20
};
int[] a;
int[] b;
static int[] c=new int[5];
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=RandomizeArray(images);
b=RandomizeArray(imageViews);
for(int j=0;j<4;j++){
int imv = b[j];
int im = a[j];
ImageView iv = (ImageView)findViewById(imv);
iv.setImageResource(im);
c[j]=a[j];
}
new CountDownTimer(7000, 1000) {
TextView timertext=(TextView)findViewById(R.id.textView1);
public void onTick(long millisUntilFinished) {
timertext.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
timertext.setText("done!");
finish();
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}.start();
}
public static int[] RandomizeArray(int[] array){
Random rgen = new Random(); // Random number generator
for (int i=0; i<array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
return array;
}
}
SecondActivity.java:
public class SecondActivity extends Activity{
int[] a;
int[] b;
int p=0;
int im;
int imv;
int[] c=new int[4];
int j;
int correct=0;
int wrong=0;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv=(TextView)findViewById(R.id.textView1);
a=RandomizeArray(MainActivity.images);
b=RandomizeArray(MainActivity.imageViews);
for(j=0;j<4;j++){
imv = b[j];
im = a[j];
ImageView iv = (ImageView)findViewById(imv);
iv.setImageResource(im);
}
}
public void onClick(View v){
for(p=0;p<4;p++){
int s=c[p];
if(((ImageView) v).getDrawable().getConstantState().equals(getResources().getDrawable(MainActivity.images[s]).getConstantState())){
correct++;
}
else{
wrong++;
}
v.setBackgroundResource(R.drawable.vattom_2);
tv.setText("Correct="+Integer.toString(correct)+" Wrong="+Integer.toString(wrong));
}
}
public static int[] RandomizeArray(int[] array){
Random rgen = new Random(); // Random number generator
for (int i=0; i<array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
return array;
}
}
Solution
You should consider using tags, I believe it would solve your problem as they're easily comparable
Answered By - Pablo_Cassinerio
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.