简体   繁体   English

将动态字符串传递给活动

[英]Passing dynamic string to activity

I create an activity with dynamic buttons in a loop. 我在循环中创建了一个带有动态按钮的活动。 I get a list and create a button for each element in the list. 我得到一个列表并为列表中的每个元素创建一个按钮。 The buttons go to the same activity afterward, but with each button I want to pass different string. 之后按钮会转到相同的活动,但是每个按钮我想要传递不同的字符串。

I did this in the loop: 我在循环中做了这个:

    tour_button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(TourListActivity.this,
                    TourMenuActivity.class);
            String info = tour.toString();
            intent.putExtra(TOUR_INFO, info);
            startActivity(intent);
        }
    }); 

But at the end, all the buttons get the same string (the string of the last button). 但最后,所有按钮都获得相同的字符串(最后一个按钮的字符串)。

======================================== full code: ========================================完整代码:

   try {
        JsonObject respObject = jsonParser.parse(response).getAsJsonObject();
        JsonArray tourListArray = respObject.getAsJsonArray("tours");
        System.out.println("tourListArray: " + tourListArray.toString());

        for(int i = 0; i < tourListArray.size(); i++){
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.VERTICAL);
            tour = tourListArray.get(i).getAsJsonObject();
            String tourCode = tour.get("tourcode").getAsString();
            Button tour_button = new Button(this);  
            tour_button.setText("Tour Code: " + tourCode);
            tour_button.setGravity(Gravity.LEFT);
            tour_button.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(TourListActivity.this,
                            TourMenuActivity.class);
                    String info = tour.toString();
                    intent.putExtra(TOUR_INFO, info);
                    startActivity(intent);
                }
            }); 


            ll.addView(tour_button);

            LinearLayout yourLL = (LinearLayout) findViewById(R.id.Tours_List);
            yourLL.setOrientation(LinearLayout.VERTICAL);
            yourLL.addView(ll);  


        }
    } catch (JsonIOException e) {
        e.printStackTrace();
    } 

When you create the button you can: 创建按钮时,您可以:

 button.setTag(someString); 

and then in the onClick you can: 然后在onClick中你可以:

public void onClick(View v) {
        Intent intent = new Intent(TourListActivity.this,
                TourMenuActivity.class);
        String info = tour.toString();
        intent.putExtra(TOUR_INFO, ((Button)v).getTag());
        startActivity(intent);
    }

The variable tour is defined outside the loop, so each button share the same variable . 变量tour在循环外定义,因此每个按钮共享相同的变量 At each iteration, you just change the reference stored by this variable. 在每次迭代中,您只需更改此变量存储的引用

You could to create a final variable inside your loop, and use it inside the OnClickListener : 您可以在循环中创建最终变量,并在OnClickListener使用它:

for (int i = 0; i < tourListArray.size(); i++) {
    ...
    final String tourInfo = tour.info;
    tour_button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(
                TourListActivity.this, 
                TourMenuActivity.class
            );
            intent.putExtra(TOUR_INFO, tourInfo);
            startActivity(intent);
        }
    });
    ...
}

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

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