Reputation: 11
I need help, I have tables 3x3. How to make reload image in gridView after click? I want make simple application 1 good click = 1 point.
public void losowanieTablicy() {
int[] str_names = {0, 1, 2, 3, 4, 5, 6, 7, 8};
shuffle(str_names);
shuffle(imgIds);
for (int i = 0; i < imgIds.length; i++) {
ImageView img = (ImageView) findViewById(imgIds[i]);
final String str = "img_" + str_names[i]; // zakladajac ze str_names.length == imgIds.length
img.setImageDrawable(
getResources().getDrawable(getResourceID(str, "mipmap", getApplicationContext()))
);
}
}
public void shuffle(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
// between i and n-1
int r = i + (int)(Math.random() * (n - i));
int tmp = a[i]; // swap
a[i] = a[r];
a[r] = tmp;
}
}
Upvotes: 0
Views: 87
Reputation: 2121
You can add an OnClick like this:
final ImageView img = (ImageView) findViewById(imgIds[i]);
img.setClickable(true);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img.setImageDrawable(
getResources().getDrawable(getResourceID(str, "mipmap", getApplicationContext()))
);
}
});
Upvotes: 1