I have a MainActivity
with 30 buttons (Ids: imageButton1
,imageButton2
...). On the click event, I'm starting a new activity called KlikNaDugme
:
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
buttons = new ImageButton[30];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = (ImageButton) findViewById(R.id.imageButton + i);
vr = i;
buttons[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(HomeActivity.this, KlikNaDugme.class).putExtra("vrijednost", vr);
startActivity(myIntent);
}
});
}
}
I'm trying to pass vr
to the KlikNaDugme
activity, which is declared as a public int
.
KlikNaDugme Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_klik_na_dugme);
int x = getIntent().getExtras().getInt("vrijednost");
System.out.println(x);
}
The problem is that it always gets a value of 29. How can I pass the id correctly?