ゲームの難易度に基づいて、動的な数の画像ビューを作成しようとしています。onclick
必要な数の画像ビューを動的に作成し、リスナーでクリック可能に設定します。私の問題は、どの画像ビューをクリックしても、最後の画像しか変更されないことです。onclick
クリックしている画像ビューをリスナーに知らせて、特定の画像ビューを変更させる方法はありますか?
public class MainActivity extends Activity {
private static int ROWS;
private static int COLUMNS;
private int gameDifficulty;
private Drawable backCard;
private RelativeLayout mainView;
private ImageView iView;
private LinearLayout layout;
private ClickListener clickListener = new ClickListener();
private ArrayList<Drawable> cards = new ArrayList<Drawable>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LoadImages();
mainView = (RelativeLayout)findViewById(R.id.mainView);
Button easy = (Button) findViewById(R.id.button1);
easy.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
gameDifficulty = 1;
StartGame();
}
});
Button normal = (Button) findViewById(R.id.button2);
normal.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
gameDifficulty = 2;
StartGame();
}
});
Button hard = (Button) findViewById(R.id.button3);
hard.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
gameDifficulty = 3;
StartGame();
}
});
}
public void LoadImages()
{
backCard = getResources().getDrawable(R.drawable.star);
cards.add(getResources().getDrawable(R.drawable.test1));
cards.add(getResources().getDrawable(R.drawable.test2));
cards.add(getResources().getDrawable(R.drawable.test3));
cards.add(getResources().getDrawable(R.drawable.test4));
cards.add(getResources().getDrawable(R.drawable.test5));
cards.add(getResources().getDrawable(R.drawable.test6));
}
public void StartGame()
{
mainView.removeView(findViewById(R.id.button1));
mainView.removeView(findViewById(R.id.button2));
mainView.removeView(findViewById(R.id.button3));
if (gameDifficulty == 1)
{
ROWS = 4;
COLUMNS = 2;
CreateCard(ROWS, COLUMNS);
System.out.println("Creating card");
}
if (gameDifficulty == 2)
{
ROWS = 4;
COLUMNS = 3;
CreateCard(ROWS, COLUMNS);
}
if (gameDifficulty == 3)
{
ROWS = 4;
COLUMNS = 4;
CreateCard(ROWS, COLUMNS);
}
}
public void CreateCard(int rows, int columns)
{
//Amount of stars going down
for (int x = 0; x < columns; x++)
{
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//Number of rows
for (int y = 0; y < rows; y++)
{
iView = new ImageView(this);
iView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
iView.setBackgroundDrawable(backCard);
iView.setId(y + 1 + (x * 4));
iView.setClickable(true);
iView.setOnClickListener(clickListener);
row.addView(iView);
}
layout.addView(row);
setContentView(layout);
}
}
class ClickListener implements OnClickListener
{
@Override
public void onClick(View v)
{
System.out.println("Clicking card");
System.out.println(iView.getId());
iView.setBackgroundDrawable(cards.get(2));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}