My question is regarding the onClick()
method, in which the "Cards" receive their images through a switch statement.
If you click the first "card", the "firstCard" variable is successfully associated with the image.
If you choose the second card, it is instantly checked, if the cards are equal without passing the image to the view (or something like that).
If they're not equal, they both get reset to the original image.
How do i get the second card image to show without the need to put an additional user input between the choice of the second card and the comparison of the two card variables?
The buttons are added dynamically, so i didn't work with XML at all. I don't know if dynamically added ressources can be manipulated with XML. I don't think so.
This is the affected part of the code:
} else {
secondCard = (ImageButton) v;
chooseCardimage(secondCard);
num_tries++;
}
((TextView)findViewById(R.id.tv1)).setText("Versuche: "+num_tries);
if (secondCard != null) {
if (firstCard.getId() == secondCard.getId()
&& !checksamecards(firstCard, secondCard)) {
try {
Thread.sleep(800);
} catch (InterruptedException e) {
Log.i("InterruptedException e: ", e.toString());
}
firstCard.setVisibility(View.INVISIBLE);
secondCard.setVisibility(View.INVISIBLE);
} else if (firstCard.getId() != secondCard.getId()
&& !checksamecards(firstCard, secondCard)) {
try {
Thread.sleep(800);
} catch (InterruptedException e) {
Log.i("InterruptedException e: ", e.toString());
}
firstCard.setImageResource(R.drawable.backimage);
secondCard.setImageResource(R.drawable.backimage);
}
firstCard = null;
secondCard = null;
}
This is the whole code:
package com.VS.memorycardgame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private int num_rows;
private static int num_tries;
private int num_cards;
private Context context;
private ArrayList<ImageButton> cards;
private ArrayList<TableRow> rows;
private TableLayout tablelayout;
private ImageButton firstCard;
private ImageButton secondCard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tablelayout = (TableLayout) findViewById(R.id.tableLayout01);
context = tablelayout.getContext();
num_cards = 8;
num_rows = 4;
num_tries = 0;
cards = new ArrayList<ImageButton>();
rows = new ArrayList<TableRow>();
newGame();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void randomize(ArrayList a) {
long seed = System.nanoTime();
Collections.shuffle(a, new Random(seed));
}
private void newGame() {
loadCards();
loadCards();
randomize(cards);
createRows();
}
private void createRows() {
try {
for (int i = 0; i < num_rows; i++) {
rows.add((TableRow) findViewById(R.id.class.getField(
"tableRow" + (i + 1)).getInt(0)));
}
createGrid();
} catch (Exception e) {
Log.i("Fehler bei Hinzufügen von Rows: ", e.toString());
}
}
private void createGrid() {
int v = 0;
int i = 0;
for (i = i; i < 16; i++) {
rows.get(v).addView(cards.get(i));
if (i == 3) {
v++;
}
if (i == 7) {
v++;
}
if (i == 11) {
v++;
}
}
}
private void loadCards() {
try {
for (int i = 0; i < num_cards; i++) {
ImageButton button = new ImageButton(context);
button.setId(100 + i);
button.setImageResource(R.drawable.backimage);
button.setOnClickListener(this);
cards.add(button);
}
} catch (Exception e) {
Log.i("Fehler bei Hinzufügen von Cards: ", e.toString());
}
}
@Override
public void onClick(View v) {
if (firstCard == null) {
firstCard = (ImageButton) v;
chooseCardimage(firstCard);
} else {
secondCard = (ImageButton) v;
chooseCardimage(secondCard);
num_tries++;
}
((TextView)findViewById(R.id.tv1)).setText("Versuche: "+num_tries);
if (secondCard != null) {
if (firstCard.getId() == secondCard.getId()
&& !checksamecards(firstCard, secondCard)) {
try {
Thread.sleep(800);
} catch (InterruptedException e) {
Log.i("InterruptedException e: ", e.toString());
}
firstCard.setVisibility(View.INVISIBLE);
secondCard.setVisibility(View.INVISIBLE);
} else if (firstCard.getId() != secondCard.getId()
&& !checksamecards(firstCard, secondCard)) {
try {
Thread.sleep(800);
} catch (InterruptedException e) {
Log.i("InterruptedException e: ", e.toString());
}
firstCard.setImageResource(R.drawable.backimage);
secondCard.setImageResource(R.drawable.backimage);
}
firstCard = null;
secondCard = null;
}
}
private boolean checksamecards(View first, View second) {
if (first == second) {
Log.i("Userpressedsame: ", "User pressed same button!");
return true; // the user pressed the same card
}
return false;
}
private void chooseCardimage(View v) {
ImageButton b = (ImageButton) v;
switch (b.getId()) {
case 100:
b.setImageResource(R.drawable.card1);
break;
case 101:
b.setImageResource(R.drawable.card2);
break;
case 102:
b.setImageResource(R.drawable.card3);
break;
case 103:
b.setImageResource(R.drawable.card4);
break;
case 104:
b.setImageResource(R.drawable.card5);
break;
case 105:
b.setImageResource(R.drawable.card6);
break;
case 106:
b.setImageResource(R.drawable.card7);
break;
case 107:
b.setImageResource(R.drawable.card8);
break;
}
}
}
UPDATE
I initially forgot the chooseCardimage() method call for secondCard, but the issue still persists.