0

I have a for loop in Android. I create Buttons and button click events in loop. The codes are in below.

The codes:

for(ii = 0; ii < yardimarray.size(); ii++){

            Button yardimmtv = new Button(yardim.this);
            yardimmtv.setText(yardimarray.get(ii));
            yardimmtv.setTextColor(Color.WHITE);
            yardimmtv.setTextSize(17);
            yardimmtv.setLayoutParams(yardimtvlp);
            yardimmtv.setBackgroundResource(tvalue.resourceId);
            yardiml.addView(yardimmtv);

            iii = ii + 1;

            yardimmtv.setOnClickListener ( new View.OnClickListener () {
                public void onClick (View yardimmv){

                    Intent yardimbgecis = new Intent(yardim.this, yardimbilgi.class);
                    yardimbgecis.putExtra("yardimid", String.valueOf(iii));
                    finish();
                    startActivity(yardimbgecis);

                }
            });

        }

But yardimid is always 9 and loop run 9 times.

How I can resolve this problem?

I need your help.

Chandan Sharma
  • 2,803
  • 1
  • 17
  • 25

1 Answers1

0

Try setting on yout Button, the position as a TAG. Something like yardimmtv.setTag(ii); And then onClick you get the tag of the button clicked using v.getTag() and put in Extras

rosu alin
  • 5,674
  • 11
  • 69
  • 150