简体   繁体   中英

Android Pass value to another activity

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?

Store the value of i in the button's tag:

    buttons = new ImageButton[30];

    for(int i=0; i<buttons.length; i++) {
        {
            buttons[i] = (ImageButton) findViewById(getResources().getIdentifier("imageButton" + (i + 1), "id", this.getPackageName()));
            buttons[i].setTag(i);

            buttons[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {

                    Intent myIntent = new Intent(HomeActivity.this, KlikNaDugme.class)
                    myIntent.putExtra("vrijednost", Integer.parseInt(view.getTag().toString()));
                    startActivity(myIntent);
                }
            });
        }
    }

The problem lies here:

buttons[i] = (ImageButton) findViewById(R.id.imageButton + i);

What you are doing is that you're creating an invalid resource reference and then finding your view upon that reference, which is obviously wrong. Remember findViewById() needs a resource id which is generated by Android Studio itself.

The solution is to save all of your 20-30 views references in an array and then use a loop to set click listeners on them

Add the vr value as tag to button and then use getTag() method to get the vr value inside onclicklistener

            for(int i=0; i<buttons.length; i++) {
            {
                buttons[i] = (ImageButton) findViewById(R.id.imageButton + i);
                vr = i;
                buttons[i].setTag(vr); // This will se the vr value as tag
                buttons[i].setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        int vr = (int)view.getTag()  // this will get the vr value from view
                        Intent myIntent = new Intent(HomeActivity.this,
                                KlikNaDugme.class).putExtra("vrijednost", vr);
                        startActivity(myIntent);
                    }
                });
            }

R.id.what_ever will give you an integer value that generated by android studio in R class not the what_ever string. You should use getResources().getIdentifier() to get correct id of resource and pass it to findViewById() .

Please try this code. It will get you the correct id of ImageButtons:

    buttons = new ImageButton[30];

    for(int i=0; i<buttons.length; i++) {
        {
            String buttonID = "imageButton" + (i+1);
            int resID = getResources().getIdentifier(buttonID, "id", getPackageName());

            buttons[i] = (ImageButton) findViewById(resID);
            buttons[i].setTag(i);
            //vr = i;
            buttons[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View args0) {

                    Intent myIntent = new Intent(HomeActivity.this,
                            KlikNaDugme.class).putExtra("vrijednost", (int)args0.getTag());
                    startActivity(myIntent);
                }
            });
        }

Enjoy it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM