简体   繁体   English

Android将值传递给另一个活动

[英]Android Pass value to another activity

I have a MainActivity with 30 buttons (Ids: imageButton1 , imageButton2 ...). 我有一个带有30个按钮的MainActivity (标识: imageButton1imageButton2 ...)。 On the click event, I'm starting a new activity called KlikNaDugme : 在click事件上,我正在启动一个名为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 . 我正在尝试将vr传递给KlikNaDugme活动,该活动被声明为public int

KlikNaDugme Activity KlikNaDugme活动

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? 问题在于,它始终获得29的值。如何正确传递ID?

Store the value of i in the button's tag: i的值存储在按钮的标签中:

    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. 请记住, findViewById()需要一个由Android Studio本身生成的资源ID。

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 解决方案是将所有20-30个视图引用保存在一个数组中,然后使用循环在其上设置单击侦听器

Add the vr value as tag to button and then use getTag() method to get the vr value inside onclicklistener 将vr值添加为按钮的标签,然后使用getTag()方法在onclicklistener中获取vr值

            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. R.id.what_ever将为您提供由Android Studio在R类中生成的整数值,而不是what_ever字符串。 You should use getResources().getIdentifier() to get correct id of resource and pass it to findViewById() . 您应该使用getResources().getIdentifier()来获取正确的资源ID,并将其传递给findViewById()

Please try this code. 请尝试此代码。 It will get you the correct id of ImageButtons: 它将为您提供正确的ImageButtons ID:

    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. 好好享受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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